0

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?

6
  • Db structure ? Db data ? Commented Sep 20, 2013 at 8:57
  • You can't show multiple files within one PHP file with header("Content-type: image/jpeg"); Commented Sep 20, 2013 at 8:58
  • first image is displayed because of mysql_result($result, 0); Commented Sep 20, 2013 at 8:59
  • When you request an url form a web server you normally request only one file, because your header is just Content-type: image/jpeg the result is also just one file. While multipart responses exist, i currently don't know how well supported they are. Commented Sep 20, 2013 at 8:59
  • "image" is a filename ? Commented Sep 20, 2013 at 9:00

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 @

mysql_result

NOTE : Please don't use mysql* functions in your new code they are depriciated in newer versions use mysqli* or PDO

4 Comments

mysql() is deprecated
echo <br>; and header("Content-type: image/jpeg"); are you kidding?
@t.niese i tried to point mysql_result($result, 0); mistake there are also others
@dianuj sure, but if you post an answer what should be changed and copying the invalid code as is and just fixing one part, it would be misleading also for others reading this answer. I think it is better to leave the whole invalid code block out an just state what needs to be changed. Or show a correct code block.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.