Displaying images from mysql database

Reply

Join Date: Dec 2003
Posts: 7
Reputation: okonjiaustin is an unknown quantity at this point 
Solved Threads: 0
okonjiaustin okonjiaustin is offline Offline
Newbie Poster

Displaying images from mysql database

 
0
  #1
Feb 12th, 2008
I have successfully uploaded images into mysql database, but now I want to display the images alongside other row data in html page.

Thanks for your previous help.

Austin.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 84
Reputation: johnsquibb is an unknown quantity at this point 
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

Re: Displaying images from mysql database

 
0
  #2
Feb 12th, 2008
something along these lines...you'll need to tweak it to fit your script, but you should get the idea...

  1. //connect to mysql with your variables here...
  2. //execute your sql query(s), store result in $result
  3.  
  4. //make a table to hold images and data
  5. $output = "<table width=\"300\">";
  6.  
  7. //fetch each row of data
  8. while($row = mysql_fetch_array($result))
  9. {
  10. //do something with each row of data
  11. //refer to columns holding data as $row['columnName'];
  12.  
  13. //for example...
  14.  
  15.  
  16. //fill table with data...be sure to refer to the column/field names in your database table!
  17.  
  18. $output .= ("
  19. <tr>
  20. <td><img src =\" ". $row['imageURL'] . "\"></td>
  21. <td>". $row['imageTitle'] . "</td>
  22. </tr>
  23. ")
  24.  
  25.  
  26. }
  27.  
  28. //close table tag
  29. $output .= "</table>";
  30.  
  31. //display output
  32. echo $output;
Last edited by johnsquibb; Feb 12th, 2008 at 3:17 pm. Reason: forgot to put image tag!
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 7
Reputation: okonjiaustin is an unknown quantity at this point 
Solved Threads: 0
okonjiaustin okonjiaustin is offline Offline
Newbie Poster

Re: Displaying images from mysql database

 
0
  #3
Feb 13th, 2008
Thanks Johnsquibb for the reply,

Please see my code that I used to insert the image and other data. The image is inserted directly into the database.

<?php
$errmsg = "";
if (! @mysql_connect("localhost","root","")) {
$errmsg = "Cannot connect to database";
}
@mysql_select_db("adim");

if(isset($_REQUEST['submit']))
{

$imgtype=$_FILES['uploadfile']['type'];
$name=$_REQUEST['name'];
$address=$_REQUEST['address'];
$dateofbirth=$_REQUEST['dateofbirth'];



if($imgtype=="image/jpeg" || $imgtype=="image/jpg" || $imgtype=="image/pjpeg" || $imgtype=="image/gif" || $imgtype=="image/x-png" || $imgtype=="image/bmp")
{

$image=$_FILES['uploadfile']['tmp_name'];
$fp = fopen($image, 'r');
$content = fread($fp, filesize($image));
$content = addslashes($content);
fclose($fp);
$sql="insert into img_tab1 (name,image,address,dateofbirth) values ('$name','$content','$address','$dateofbirth')";
$res=mysql_query($sql) or die (mysql_error());
}
}
?>

Please further suggestions to display the whole row data including the image will be appreciated.

Austin
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 561
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is online now Online
Posting Pro

Re: Displaying images from mysql database

 
0
  #4
Feb 13th, 2008
You can insert in the image table the path where you have saved your picture.Then display it like:

  1. <img src =\" ". $row['imageURL'] . "\">
  2. //where the value of $row['imageURL'] is the path of the image like "www.domain.com/pics/pic1.jpg"
  3.  
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 84
Reputation: johnsquibb is an unknown quantity at this point 
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

Re: Displaying images from mysql database

 
0
  #5
Feb 13th, 2008
i usually just insert the path to the location of the image, so I'm not well versed in storing the actual image data in and retrieving it from the database...I stumbled across this tutorial on google, however, so maybe it will be of use to you...

http://www.spoono.com/php/tutorials/tutorial.php?id=42
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 561
Reputation: ryan_vietnow is an unknown quantity at this point 
Solved Threads: 71
ryan_vietnow's Avatar
ryan_vietnow ryan_vietnow is online now Online
Posting Pro

Re: Displaying images from mysql database

 
0
  #6
Feb 13th, 2008
yes we can insert the image data but it slows down the execution of the script and have high data filesize.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,744
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Displaying images from mysql database

 
0
  #7
Feb 13th, 2008
Its not a good idea to store images in the database. As Ryan_vietnow has mentioned, it will add on to the server load, slows down the execution of the script and takes a lot of time to display it if you have more (say 100 images ) per page!
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC