Jump to content

PHP/SQL Database image array?


Kez323

Recommended Posts

Hey, so to display an image from a database I was using

$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT * FROM store_image WHERE id=$id");

$image = mysql_fetch_assoc($image);

$image = $image['image'];

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

That worked fine to return an image via matching the id.

 

But I want to get  ALL images from the table.. 

 

I tried this:

$image = mysql_query("SELECT * FROM store_image");

while($image = mysql_fetch_array($image)){
	

$image = $image['image'];

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


But did not work.. what am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/281170-phpsql-database-image-array/
Share on other sites

you cannot just output multiple images. each image must have an <img src=' ... ' alt=''> html tag. the URL you put into the src=' ... ' attribute must result in the correct content type being output, followed by the data for one image.

 

to display all your images, you would need to retrieve all the id's and output each one on the end of the url in the src= ' ... ' attribute it its own <img > tag. then when the browser fetches each image in the <img > tags it finds on a page, your first script will get the id, retrieve the data, output the content type header, followed by the image data for that id.

Assuming your original file is called image.php and you have a column in the table called name, then something like this in another file:

$result = mysql_query("SELECT id, name FROM store_image");
 
while($row = mysql_fetch_array($result)) {
	echo '<img src="image.php?id='.$row['id'].'" alt="'.$row['name'].'"><br />';
}

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.