Jump to content

Help displaying images in loop stored in mysql database row


bdarilek

Recommended Posts

Help!! I'm a newbie with a problem displaying my images that are stored in the database.

 

I can upload and actually view an image but from only the first row.

 

The information from the other rows displays just fine. I just can't get the right image to display with the rest of the corresponding fields. It only show the image from the first record.

 

Below is my code:

 

<?php

 

$dbcnx = mysql_connect("localhost", "xxxxxxxx", "xxxxxx");

mysql_select_db("xxxxxxx");

 

$request = @mysql_query("select * from profiles");

while ($row = @mysql_fetch_assoc($request)) {

 

 

if ($_REQUEST[gim] == 1) {

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

        print $row['c_photo'];

        exit ();

        }

 

echo

"<img src=?gim=1><br>".

"Name :{$row['c_first']} {$row['c_last']}<br>".

"Email :{$row['c_email']} <br>".

"Phone :{$row['c_phone']} <br><hr><br><br><br>";

 

 

}

 

 

?>

 

Thanks for any help in advance.

You shouldn't be storing the actual image in your database - you should store the path to where it is on your server. You then echo out an image tag with the path, which can be done inside the loop just like the other data.

Exactly as GingerRobot stated. Databases don't handle the storing of large file sizes too easily. Displaying images takes longer. Deleting images from the database causes fragmentation. In fact, some web hosts are against it altogether.

Sorry, I wasn't being very clear:

 

Upload image to a directory on the actual file system.

Take note of where it is and what it is, and store it in your database.

 

Then, for example:

$get_image = mysql_query("SELECT image_directory FROM images WHERE image_id = $example") or die(mysql_error());
$image_directory = mysql_fetch_row($get_image);
echo '<img src="'.$image_directory.'" alt="Whatever, maybe a caption?" />';

 

The column image_directory could hold the location of an image on your web server. So, image_directory could be:

 

"images/example.gif".

 

Whoops:

 

$get_image = mysql_query("SELECT image_directory FROM images WHERE image_id = $example") or die(mysql_error());
$image_directory = mysql_fetch_row($get_image);
echo '<img src="'.$image_directory[0].'" alt="Whatever, maybe a caption?" />';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.