I am able to retrieve single image from mysql database using php.
Now I want to retrieve multiple images.
Here is the piece of code:
<?php
--db connection code--
$sql = "SELECT image FROM try WHERE status='0'";
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
while ($row = @mysql_fetch_assoc($result))
{
// set the header for the image
header("Content-type: image/jpeg");
echo mysql_result($result, 0);
echo '<br>';
}
?>
executing this code, only first image in table is being displayed. How to retrieve multiple images?
You are using Content-type: image/jpeg
, which means it can only return one image, as the browser will only expect one. If you want to return multiple images to download you have to do some workaround.
One option would be to first pack the files together into one file (like a ZIP archive), and push that to the user.
Or if you don't want to push this as a download, then create a HTML page, with image links to your images. Each of the link will only return one of course.
You need to pass the loop counter in result_mysql
$count=0;
while ($row = @mysql_fetch_assoc($result))
{
// set the header for the image
header("Content-type: image/jpeg");
echo mysql_result($result,$count);
$count++;
}
Please don't use the error suppressor @
NOTE : Please don't use mysql* functions in your new code they are depriciated in newer versions use mysqli* or PDO
As the Content-type: image/jpeg returns only a single image, you should consider saving the paths of the images in your database table. Later on you won't have any problems fetching all of them and echoing the source elements in your html.