Jump to content

Displaying all the stored images from mysql table[URGENT]

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
21 replies to this topic

#1
Astha

Astha

    Newbie

  • Members
  • PipPip
  • 25 posts
Hi,

I got a script which displays the image from mysql table.

Here's my script:
testgetpicture.php
<?php

    // just so we know it is broken

    error_reporting(E_ALL);

    // some basic sanity checks

    if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {

        //connect to the db

        $link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());

 

        // select our database

        mysql_select_db("templetree") or die(mysql_error());

 

        // get the image from the db


        $sql = "SELECT content FROM sample_design WHERE image_id=".$_GET['image_id'];

 

        // the result of the query

        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

 

        // set the header for the image

        header("Content-type: image/jpeg");

        echo mysql_result($result, 0);

 

        // close the db link

        mysql_close($link);

    }

    else {

        echo 'Please use a real id number';

    }

?> 

But it allows me to display image only by specifying its id.

EX. display_image.html

<html>

<body>

<img src="testgetpicture.php?image_id=1"/>

</body>

</html>


This way i'll have to display each image by specifying its image id individually. Is there any way where I can display all the images (from database table) in a row or column without specifying a specific image id?

Please let me know whether it is possible or not.


Thanks in advance.

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
just ask the same table in your calling page and create one <img> for each picture found...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
Astha

Astha

    Newbie

  • Members
  • PipPip
  • 25 posts
Sorry, didn't get your point...can you clarify more on this.

I want to display all of the images stored in database, by using mysql_num_rows or something like that.

Thanks,
Astha

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
If I get you right, you wish to display all images within the table example_design? This should do the job, note how I use a while loop to iterate through the array.
<?php
    // just so we know it is broken
    error_reporting(E_ALL);

    //connect to the db
    $link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
 
    // select our database
    mysql_select_db("templetree") or die(mysql_error());
 
    // get the rows from the db
    $sql = "SELECT content FROM sample_design";
 
    // the result of the query
    $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
 
    while($row = mysql_fetch_array( $result )) {
        // Print out the contents of each row into a tag
        echo "<img src=\"{$row['image_id']}\"/><br/>";
    }

    // close the db link
    mysql_close($link); 
?>

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
you're missing out a few important things here, nullw0rm. At first, that is how the display_images.html would be when it's transformed to display_images.php, and additionally, the img src attribute needs the "testgetpicture.php?image_id=" part as well. Btw, why escaping the quotation marks? it's much easier to use aphostrofes then, at least for readability...
echo '<img src="testgetpicture.php?image_id='.$row['image_id'].'"/><br/>';

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
Astha

Astha

    Newbie

  • Members
  • PipPip
  • 25 posts
Hi Nullw0rm and Orjan,

I tried your codes (both echo statements) and opened the same file to see the output (didn't use another file as reference). It gives me a notice (with small box below it) times the number of mysql entries:

Quote

Notice: Undefined index: image_id in C:\wamp\www\Templetree\trial\testgetpicture_all.php on line 19

And it is pointing echo "<img src" line.

Just noticed that in my original code i had used below line:
 header("Content-type: image/jpeg"); 

In your code this line is missing. Am I not supposed to use that?

Thanks,
Astha

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
Astha, you're editing the wrong file. let your testgetpicture.php be as it is. it shall not be modified in any way from your original one.

it's your file display_images.html that needs to be changed into an php file according to nullw0rms file, but with my echo statement.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
Astha

Astha

    Newbie

  • Members
  • PipPip
  • 25 posts
Hello Orjan,

As per your instructions I kept my testgetpicutre.php file as it is and modified display_image.html to display_image.php. But I'm still getting the same notice.

Thanks,
Astha

#9
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
how does you files look like right now?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#10
Astha

Astha

    Newbie

  • Members
  • PipPip
  • 25 posts
Hi Orjan,

Here's my testgetpicture.php file:

<?php

    // just so we know it is broken

    error_reporting(E_ALL);

    // some basic sanity checks

    if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {

        //connect to the db

        $link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());

 

        // select our database

        mysql_select_db("templetree") or die(mysql_error());

 

        // get the image from the db


        $sql = "SELECT content FROM sample_design WHERE image_id=".$_GET['image_id'];

 

        // the result of the query

        $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

 

        // set the header for the image

        header("Content-type: image/jpeg");

        echo mysql_result($result, 0);

 

        // close the db link

        mysql_close($link);

    }

    else {

        echo 'Please use a real id number';

    }

?>


And her's my display_image.php file:

<html>

<body>

<?php

    // just so we know it is broken

    error_reporting(E_ALL);


    //connect to the db

    $link = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());

 

    // select our database

    mysql_select_db("templetree") or die(mysql_error());

 

    // get the rows from the db

    $sql = "SELECT content FROM sample_design";

 

    // the result of the query

    $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

 

    while($row = mysql_fetch_array( $result )) {

        // Print out the contents of each row into a tag

       echo '<img src="testgetpicture.php?image_id='.$row['image_id'].'"/><br/>';  

    }


    // close the db link

    mysql_close($link); 

?> </body>

</html>

I still get the same notices (times the number of mysql entries) with small boxes below them

#11
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
what is the source of the webpage display_image.php generates?
what does your sql table look like?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#12
Astha

Astha

    Newbie

  • Members
  • PipPip
  • 25 posts
I've attached screenshots of mysql table and display_image.php output.

Thanks,
Reshma

Attached Files