Pull images from MySQL into HTML

KENNYB

2[H]4U
Joined
Jul 26, 2004
Messages
3,147
I'm looking for a way to place multiple images from a MySQL database and place them into a webpage. Every resource i've found only works on one image (while i want to display many). The jpegs are stored as BLOBS.

Here's what i've done, after setting up a connection to the DB:

/* Query the database */
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());
$row = mysql_fetch_assoc($result);
$pic = $row['image']; // image is the field name where the BLOB is stored

If i change the output headers to :
header("Content-type: image/jpeg");

I can display the image by:
echo $pic;

That is good for only one image. Nothing else. I'd like to ouput multiple images and text. If i change the output headers to text/html, I will see binary output to the browser where the image should be. I'd imagine i have to convert the binary stream to a something else, or convert the binary stream to a file and do a simple <IMG SRC align="filename.jpg"> tag.

Help, please...
 
Why do want to store the images as BLOB versus just storing the metadata in the DB and the image physically on the file system?

Cobalt2112.png
 
Oh, i know, that would have been much easier. I'm doing a project for a friend of mine and he doesn't want the images sitting in a folder where anyone can view them (albeit by guessing names). I could make it really difficult for someone to guess filenames and then not letting anyone pull up a directory listing.
 
That's what I thought your answer would be. How about another table which holds a compound primary key for your images?

Example:

Table: ProductImage
Column: ProductID
Column: ImageBLOB

Where you can select * from ProductImage where ProductID = n

It'll return multiple rows and you just have to loop thru the recordset.

ProductID can be the ROWNUM or any other distict data that can be used distinguish all your images.

Cobalt2112.png


Oh, i know, that would have been much easier. I'm doing a project for a friend of mine and he doesn't want the images sitting in a folder where anyone can view them (albeit by guessing names). I could make it really difficult for someone to guess filenames and then not letting anyone pull up a directory listing.
 
echo "<img src=\"images.php?pictureID=500\" >";

Ugh... that's all i needed. I don't want to say how long this took me, especially since i had "scr" and "src" mixed up...lol. I would like to keep images in a folder instead of the database. I'll give your example a shot. Thanks for the help.
 
echo "<img src=\"images.php?pictureID=500\" >";

Ugh... that's all i needed. I don't want to say how long this took me, especially since i had "scr" and "src" mixed up...lol. I would like to keep images in a folder instead of the database. I'll give your example a shot. Thanks for the help.

Put your images in a directory that is not viewable by the web, and create a script that loads that image when called upon... basically the same as the above code for the image, just you wouldn't store the image as blob but just its physical location in the other directory...

Eg:

Code:
<php echo "<img src=\"images.php?pic=1\" />"; ?>

script pulls up this image:
C:\inetpub\hiddenimages\picture1.jpg

from this page:

C:\inetpub\wwwroot\yoursite\page.php
 
That's even better. I can foresee a problem with having a lot of images in a database. Having the images in a directory would then make them very easy to modify without having to worry about uploading the modified images back to the DB.
 
Yeah I've used it for a company's site I developed and Gallery2 also uses it if you want to see an example of it.
 
Back
Top