Kryllster Posted July 22, 2010 Share Posted July 22, 2010 I am trying to store and retrieve small icon images from the database and display them in a format I think the image upload is there but I cant get it to display. Here is some of the code I used to try the first time. <?php // connection to database info here include('../database/dbconn.php'); $query = "SELECT * FROM $tbl_name"; $result = mysql_query($query); $num = mysql_num_rows($result); echo "<center><h2>Crimson Blade Skill Comment Page</h2></center>"; echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"15\">"; $i=0; while ($i < $num) { $image = mysql_result($result,$i,"image"); $skillname = mysql_result($result,$i,"skillname"); $class = mysql_result($result,$i,"class"); $type = mysql_result($result,$i,"type"); $description = mysql_result($result,$i,"description"); $name = mysql_result($result,$i,"name"); echo "<tr>"; echo "<td>"; echo "<hr>"; echo "<b>Skill Icon: </b><img src=\"$image\"><br>"; echo "<b>Skillname: </b>$skillname<br>"; echo "<b>Class: </b>$class<br>"; echo "<b>Skill Type: </b>$type<br>"; echo "<b>Description:</b>$description<br>"; echo "<b>Submitted by: </b>$name<br>"; echo "<hr>"; echo "</td>"; $i++; } echo "</table>"; echo "</center>"; echo "<hr>"; echo "<form><input type=button value=\"Return\" onClick=\"history.back()\"></form>"; ?> Any help on this is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/208592-how-to-display-image-from-database/ Share on other sites More sharing options...
cmor Posted July 22, 2010 Share Posted July 22, 2010 $query = "SELECT * FROM $tbl_name"; $result = mysql_query($query); $num = mysql_num_rows($result); while ($i < $num) { $row = $result->fetch_row(); echo "<b>Skill Icon: </b><img src=\"$row['image']\"><br>"; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/208592-how-to-display-image-from-database/#findComment-1089836 Share on other sites More sharing options...
Kryllster Posted July 22, 2010 Author Share Posted July 22, 2010 I tried several different ways to implement the above code but always got an error. Is there a better way to do this than storing the image in a database if so can I get a link or some code to show how its done. Quote Link to comment https://forums.phpfreaks.com/topic/208592-how-to-display-image-from-database/#findComment-1089856 Share on other sites More sharing options...
PFMaBiSmAd Posted July 22, 2010 Share Posted July 22, 2010 If the actual image is stored in your database table, you must make a second .php script that outputs the correct content-type: header followed by the image data. You then put the URL of this second .php script into the src= "..." attribute of the <img src="..." alt=""> tag. The src= attribute is always a URL that results in an image being output. You would also need to store the correct content-type (image/jpg, image/gif, ... in your database table or use a lookup between the extension of the image and the content type. Most people use a GET parameter on the end of the URL to tell the .php script which image to output so that they can use the same .php script to output any of their stored images. Short-answer: You don't output the binary image data in the <img tag. The Internet and browsers don't work that way. Quote Link to comment https://forums.phpfreaks.com/topic/208592-how-to-display-image-from-database/#findComment-1089860 Share on other sites More sharing options...
cmor Posted July 23, 2010 Share Posted July 23, 2010 It looked like you were just storing image links..... My previous solution would not work in your case anyhow. Replace this for your loop: while($row=mysql_fetch_array($result)) echo "<b>Skill Icon: </b><img src=\"$row['image']\"><br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/208592-how-to-display-image-from-database/#findComment-1089865 Share on other sites More sharing options...
Kryllster Posted July 23, 2010 Author Share Posted July 23, 2010 Well this doesnt work but I hope Im on the right track <?php // connection to database info here include('../database/dbconn.php'); $query = "SELECT image FROM $tbl_name"; $result = mysql_query($query, $sql); $result_data = mysql_fetch_array($result, MYSQL_ASSOC); header("Content-type: image/gif") ; echo $result_data['image']; ?> here is the other part where I try to display it. echo "<b>Skill Icon:</b> <img src=\"fetch_image.php\">"; I know this is not exactly how to do it however untill I can see a better tutorial I will just keep plugging along. Quote Link to comment https://forums.phpfreaks.com/topic/208592-how-to-display-image-from-database/#findComment-1089901 Share on other sites More sharing options...
Kryllster Posted July 23, 2010 Author Share Posted July 23, 2010 I think I need to go with storing the image in the file system then putting the url in the database does anybody have a good link to a tutorial or some code to show how this is done. I sure would appreciate it. I have looked on google and cant find anything decent. Thanks in advance, Quote Link to comment https://forums.phpfreaks.com/topic/208592-how-to-display-image-from-database/#findComment-1089954 Share on other sites More sharing options...
Kryllster Posted July 23, 2010 Author Share Posted July 23, 2010 Well I got the image to upload to a directory and put the image url in the database and can now display it. It works nice after working on this all day Im gonna take a break and get some sleep. If anybody would like to see the code for this I will gladly share just post a reply. Quote Link to comment https://forums.phpfreaks.com/topic/208592-how-to-display-image-from-database/#findComment-1090004 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.