View Image Stored in Database on Webpage

In earlier article we have check to store the image in database using the BLOB datatype. In this article we will check for how to display the images which are stored in database rather than images which are stored in specific folders.

If you have stored images in database then displaying those images is not just a task of giving the data in src tag.

For displaying these type of images we will have Image src just like below.

  <img src="/view_image.php?id=1" />
  <img src="/view_image.php?id=2" />
  <img src="/view_image.php?id=3" />

From the above code you will get that we have to create a php file which serves the based on ID which we have passed in the URL parameter.

So now create a new files with the name “view_image.php” and we have to place the below code in that file.

// some basic sanity checks if(isset($_GET["id"]) && is_numeric($_GET["id"])) { //connect to the db $link = mysql_connect("localhost", "username", "password"); if(!$link) { die("Could not connect: " . mysql_error()); } // select our database mysql_select_db("testimage") or die(mysql_error()); // get the image from the db $sql = "SELECT image FROM testimage WHERE image_id=".$_GET[id"]; // the result of the query $result=mysql_query("$sql") or die("Invalid query: " . mysql_error()); // set the header for the image // This is the line which does the trick. header("Content-type: image/jpeg"); echo mysql_result($result, 0); // close the db link mysql_close($link); } else { echo "Image ID not Set"; }
  // some basic sanity checks
  if(isset($_GET["id"]) && is_numeric($_GET["id"]))
  {
   //connect to the db
   $link = mysql_connect("localhost", "username", "password");
   if(!$link)
   {
     die("Could not connect: " . mysql_error());
   }
 
   // select our database
   mysql_select_db("testimage") or die(mysql_error());
 
   // get the image from the db
   $sql = "SELECT image FROM testimage WHERE image_id=".$_GET[id"];
 
   // the result of the query
   $result=mysql_query("
$sql") or die("Invalid query: " . mysql_error());
 
   // set the header for the image
   // This is the line which does the trick.
   header("
Content-type: image/jpeg");
   echo mysql_result($result, 0);
 
   // close the db link
   mysql_close($link);
  }
  else
  {
    echo "
Image ID not Set";
  }

Only one line is important which is header("Content-type: image/jpeg");, which instruct that the output of the view_image.php file is image and not the php code.

The line which does the trick is header(“Content-type: image/jpeg”);

So this is how you can show your images which are stored in database, if you are not sure about the earlier article about storing the image in database then you can refer to this article for the same.

Subscribe to our RSS feed to get the latest updates via mail.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: