Jump to content

PHP/SQL Database image array?


Go to solution Solved by AbraCadaver,

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.

Edited by mac_gyver
  • Solution

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 />';
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.