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

How to display 5 pictures stored in mysql using while loop.

P: 4
Hello Sirs/madam
i have 5 picture store in mysql , and now i want to display these picture on the page ,using while loop ., in php.
please let me know through very very simple code.
thanking you.
best regards.
ikram
Aug 2 '07 #1
Share this Question
Share on Google+
13 Replies


pbmods
Expert 5K+
P: 5,821
Heya, ikram. Welcome to TSDN!

You posted this in the PHP Articles section. I'll go ahead and move it to the PHP Forum where an Expert will be more likely to find it.

Changed thread title to better describe the problem.
Aug 2 '07 #2

kovik
Expert 100+
P: 1,044
Is the binary data actually saved in the database, or just the filenames?
Aug 2 '07 #3

P: 4
Is the binary data actually saved in the database, or just the filenames?
Hello sir.
these pictures stores as blob in mysql. and i wnt to display these all pictures through while loop. i will understand very very simple code.

thanks.
ikram
Aug 3 '07 #4

Tarantulus
100+
P: 114
Hello sir.
these pictures stores as blob in mysql. and i wnt to display these all pictures through while loop. i will understand very very simple code.

thanks.
ikram
Ikram,

by the sound of it you'll just need to loop through the mysql_result array

try:
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT imagename FROM yourtable";
  2. $runsql=mysql_query($sql);
  3. $i=0;
  4. $len=mysql_numrows($runsql);
  5.  
  6. while($i<$len){
  7. $image=mysql_result($runsql,$i);
  8. echo("<IMG SRC=\"$image\"></IMG>
  9. }
obviously you need to replace "imagename" with the name of your SQL field and "yourtable" with the name of the table

my coding is a bit sloppy so maybe someone else may give you a cleaner answer, but as far as I can gather that is what you want
Aug 3 '07 #5

gauravgmbhr
100+
P: 107
Ikram,

by the sound of it you'll just need to loop through the mysql_result array

try:
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT imagename FROM yourtable";
  2. $runsql=mysql_query($sql);
  3. $i=0;
  4. $len=mysql_numrows($runsql);
  5.  
  6. while($i<$len){
  7. $image=mysql_result($runsql,$i);
  8. echo("<IMG SRC=\"$image\"></IMG>
  9. }
obviously you need to replace "imagename" with the name of your SQL field and "yourtable" with the name of the table

my coding is a bit sloppy so maybe someone else may give you a cleaner answer, but as far as I can gather that is what you want

try this link and see how the while loop is used with mysql_fetch_row()
http://in2.php.net/mysql_fetch_row
Aug 3 '07 #6

kovik
Expert 100+
P: 1,044
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT imagename FROM yourtable";
  2. $runsql=mysql_query($sql);
  3. $i=0;
  4. $len=mysql_numrows($runsql);
  5.  
  6. while($i<$len){
  7. $image=mysql_result($runsql,$i);
  8. echo("<IMG SRC=\"$image\"></IMG>
  9. }
The images are sored in the database, so the <img> tag wouldn't work that way. Also, FYI, you don't need a counter variable ($i), or the number of rows.
Aug 3 '07 #7

kovik
Expert 100+
P: 1,044
In order to display data from your database as an image, you'll need to create a file dedicated to retrieving the images and use it to display.

i.e.

[php]// image.php
header('Content-Type: image/jpeg');

// Check if it's been given an image to load
if(!isset($_GET['id']))
{
echo file_get_contents('someDefaultImage.jpg');
}
else
{
// Get the data
$result = mysql_query('SELECT `imageData` FROM `imageTable` WHERE `id` = " . (int)$_GET['id'] . ";");
$data = mysql_fetch_object($result);

// Echo the data
echo $data->imageData;
}[/php]

And then, call it in your other files like this:

[html]<img src="path/to/image.php?id=1" />[/html]

You could easily turn that into a while loop where you increment the id to use.
Aug 3 '07 #8

Tarantulus
100+
P: 114
Wow, I try to help and I get schooled! wasn't expecting that :)
Aug 3 '07 #9

kovik
Expert 100+
P: 1,044
Wow, I try to help and I get schooled! wasn't expecting that :)
Hehe. Well, it's a good thing that you did. If you were to continue retrieving results in that manner, you'd be wasting valuable memory.
Aug 4 '07 #10

P: 4
Ikram,

by the sound of it you'll just need to loop through the mysql_result array

try:
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT imagename FROM yourtable";
  2. $runsql=mysql_query($sql);
  3. $i=0;
  4. $len=mysql_numrows($runsql);
  5.  
  6. while($i<$len){
  7. $image=mysql_result($runsql,$i);
  8. echo("<IMG SRC=\"$image\"></IMG>
  9. }
obviously you need to replace "imagename" with the name of your SQL field and "yourtable" with the name of the table

my coding is a bit sloppy so maybe someone else may give you a cleaner answer, but as far as I can gather that is what you want
Hello sir.
i see your code became very happ because of very smple code. but sir try this , it doesn't work. i write the whole code is.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. mysql_connect('localhost','root','')
  5. or die ("connection can't be established");
  6. mysql_select_db("test")
  7. or die("Data Base Can't be selected");
  8.  
  9. $sql="SELECT imagename FROM pix";
  10. $runsql=mysql_query($sql);
  11. $i=0;
  12. $len=mysql_numrows($runsql);
  13.  echo $len;
  14. while($i<$len)
  15. {
  16.  
  17. $image=mysql_result($runsql,$i);
  18.  
  19. echo "(<IMG SRC=\"$image\"></IMG>;
  20.  
  21. }
  22.  
  23. ?>
please you try this byself and then inform from the correct one.
thanking you.
best regards .
ikram
Aug 4 '07 #11

P: 4
Hello sir.thank you very much.
sir can you write me the complet programing code so that i display the 5 pictures saved in mysql. sir for this you also write the following codes.

1 - code for table where my pictures are stores.
2- retrive the pictures through loop from mysql (sir write complete program)
3- display this on the other page (pleae write complete program)
sir you write these codes for me, and also try by you rself to test this.

i mean you write the whole program so i copy this from here and paste in my note pade and this should work . i will set the database connection no proble.
thanking you.
ikram


thanking you sir.


In order to display data from your database as an image, you'll need to create a file dedicated to retrieving the images and use it to display.

i.e.

[php]// image.php
header('Content-Type: image/jpeg');

// Check if it's been given an image to load
if(!isset($_GET['id']))
{
echo file_get_contents('someDefaultImage.jpg');
}
else
{
// Get the data
$result = mysql_query('SELECT `imageData` FROM `imageTable` WHERE `id` = " . (int)$_GET['id'] . ";");
$data = mysql_fetch_object($result);

// Echo the data
echo $data->imageData;
}[/php]

And then, call it in your other files like this:

[html]<img src="path/to/image.php?id=1" />[/html]

You could easily turn that into a while loop where you increment the id to use.
Aug 4 '07 #12

pbmods
Expert 5K+
P: 5,821
Heya, ikram.

TSDN members are expected to write their own code. We're happy to help you out if you have specific questions, but you can't realistically expect someone to do your coding for you on a free forum.
Aug 4 '07 #13

kovik
Expert 100+
P: 1,044
sir can you write me the complet programing code so that i display the 5 pictures saved in mysql. sir for this you also write the following codes.

1 - code for table where my pictures are stores.
2- retrive the pictures through loop from mysql (sir write complete program)
3- display this on the other page (pleae write complete program)
sir you write these codes for me, and also try by you rself to test this.

i mean you write the whole program so i copy this from here and paste in my note pade and this should work . i will set the database connection no proble.
How much are you going to pay me for it? I don't work for free.
Aug 5 '07 #14

Post your reply

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