Jump to content

How to display image from database!


Kryllster

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/208592-how-to-display-image-from-database/
Share on other sites

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.

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.

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,

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.