By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

display multiple image from database in php

P: 8
i fetch single image . how i fetch multiple image. please send the code.
img1.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con=mysql_connect('localhost','root','');
  3. $d=mysql_select_db("test");
  4. $q1="select * from cat ";
  5. $r1=mysql_query($q1);
  6. while($r=mysql_fetch_array($r1))
  7. {
  8. header("Content-type: image/jpeg");
  9. print $r['image'];
  10. }
  11. ?>
img2.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con1=mysql_connect('localhost','root','');
  3. $d1=mysql_select_db("test");
  4. $q2="select * from cat";
  5. $r2=mysql_query($q2);
  6. //$r3=mysql_fetch_array($r2);
  7. ?>
  8. <table border="1">
  9. <tr>
  10.     <td>id</td>
  11.     <td>name</td>
  12.     <td>image</td>
  13.     </tr>
  14. <?php
  15. while($r3=mysql_fetch_array($r2))
  16. {
  17. ?>
  18. <tr>
  19. <td><?php echo $r3['id']; ?></td>
  20. <td><?php echo $r3['name']; ?></td>
  21. <td><img src="img4.php" height="100" width="100" /></td>
  22. </tr>
  23. <?php
  24. }
  25. ?>
  26. </table>
Mar 31 '10 #1

✓ answered by Atli

When you have PHP set the mime-type via the header function to that of an image, that requests becomes an image. Thus, you can not print more than a single image per request.

Which means that to print multiple images per page, you need to make multiple requests to the PHP script, each one representing a single image (just like you would with normal images). Usually you have two scripts, one that prints the HTML of the page, fetches the image meta-data and adds <img> tags, each one calling a second PHP script while passing it the ID of an image.

The second PHP script then just fetches and prints the data of that single image.

please send the code.
Not going to happen. We are happy to guide you through problems you may face while writing your own code, but we will not simply write it for you and hand it over.

Share this Question
Share on Google+
15 Replies


vivekgs2007
P: 60
hello,
use mysql_fetch assoc();
and using "for" loop, or "if n while" u can retrieve all the images on single click
Mar 31 '10 #2

Atli
Expert 5K+
P: 5,058
When you have PHP set the mime-type via the header function to that of an image, that requests becomes an image. Thus, you can not print more than a single image per request.

Which means that to print multiple images per page, you need to make multiple requests to the PHP script, each one representing a single image (just like you would with normal images). Usually you have two scripts, one that prints the HTML of the page, fetches the image meta-data and adds <img> tags, each one calling a second PHP script while passing it the ID of an image.

The second PHP script then just fetches and prints the data of that single image.

please send the code.
Not going to happen. We are happy to guide you through problems you may face while writing your own code, but we will not simply write it for you and hand it over.
Mar 31 '10 #3

P: 8
In which program i use for loop and mysql_fetch_assoc .
Mar 31 '10 #4

P: 8
PLEASE use my code then u send my answer .
Mar 31 '10 #5

Atli
Expert 5K+
P: 5,058
@gaurav13477
We are not going to write this for you, mate. We are here to help you write the code, not to do it for you.

Try it yourself and let us know what exactly you are having problems with.
Remember to include the details we need to figure out what you are doing. Like: error messages, excepted output, actual output, code examples.
Mar 31 '10 #6

P: 8
How i fetch multiple image from database here is my coding. i fetch single image.
where i get mistake. and which line.

img1.php
Expand|Select|Wrap|Line Numbers
  1.     <?php
  2.     $con=mysql_connect('localhost','root','');
  3.     $d=mysql_select_db("test");
  4.     $q1="select * from cat ";
  5.     $r1=mysql_query($q1);
  6.     while($r=mysql_fetch_array($r1))
  7.     {
  8.     header("Content-type: image/jpeg");
  9.     print $r['image'];
  10.    }
  11.    ?>

img2.php
Expand|Select|Wrap|Line Numbers
  1.   <?php
  2.     $con1=mysql_connect('localhost','root','');
  3.     $d1=mysql_select_db("test");
  4.     $q2="select * from cat";
  5.     $r2=mysql_query($q2);
  6.     //$r3=mysql_fetch_array($r2);
  7.     ?>
  8.     <table border="1">
  9.     <tr>
  10.        <td>id</td>
  11.        <td>name</td>
  12.        <td>image</td>
  13.       </tr>
  14.   <?php
  15.    while($r3=mysql_fetch_array($r2))
  16.    {
  17.    ?>
  18.    <tr>
  19.    <td><?php echo $r3['id']; ?></td>
  20.    <td><?php echo $r3['name']; ?></td>
  21.    <td><img src="img4.php" height="100" width="100" /></td>
  22.    </tr>
  23.    <?php
  24.    }
  25.    ?>
  26.    </table>
Apr 1 '10 #7

vivekgs2007
P: 60
tell me exactly wt u need....y u used this header("Content-type: image/jpeg");
Apr 1 '10 #8

P: 8
I get onle single image but in my database there are many other image.
I used for and mysql_fetch_assoc also but i can't get solve my problem
Apr 1 '10 #9

vivekgs2007
P: 60
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $con=mysql_connect('localhost','root','');
  3. $d=mysql_select_db("test");
  4. $q1=mysql_query("select * from cat ");
  5. $r1 = mysql_num_rows($q1);
  6. if($r1>0)
  7. {
  8. while($r=mysql_fetch_assoc($q1))
  9. {
  10.  
  11. echo $r['image'];
  12. }
  13. }
  14. ?>
  15.  
use table td tr
Apr 1 '10 #10

P: 8
I get output http://localhost/binary/pic3.php.
Instead of image
Apr 1 '10 #11

vivekgs2007
P: 60
<img src="folder_image/<?php echo $r['image']; ?>" height="100" width="100" />
Apr 1 '10 #12

P: 8
I think u can't get my question.
http://localhost/binary/pic3.php
Apr 1 '10 #13

vivekgs2007
P: 60
ok what exactly u need...tell me clearly
Apr 1 '10 #14

Atli
Expert 5K+
P: 5,058
The problem with the code above, the one you labeled "img1.php", is that you are printing all the images in the database, instead of just one.

When you set the content-type header to an image, the PHP code becomes an image and you need to print the data belonging to that image, and only that image.

Your code fetches all the images from the database and prints every one of them. The browser is only expecting data for one image, so when it gets all of your images, it can not render it correctly and just displays an error icon or the file name.

You should only be fetching and printing a single image, not all of them.
Apr 1 '10 #15

P: 8
I used two file to display image.In second file to fetch image
<img src="img1.php" height="100" width="100"> In this i get single image.
And your answer is <img src="imq/. $row['img']" > where i write this code.
And where i make folder img.
i want to fetch mutiple image from database. but from my code get single
code . I send code also to u .
Apr 3 '10 #16

Post your reply

Sign in to post your reply or Sign up for a free account.