-2

I want to show image on php from mysql database. I use the following code:

 $last_id = mysql_insert_id();
           echo "Image Uploaded. <p /> Your image <p /> <img src=get.php?id=$last_id>";
<?php
 mysql_connect("localhost","root","") or die(mysql_error());

 mysql_select_db("image") or die(mysql_error());

 $id = $_REQUEST['id'];

 $result = mysql_query("SELECT * FROM image WHERE id = $id");

 $image = $result['image'];

header("Content-type: image/jpeg");
echo $image;
?>
user2015
  • 67
  • 1
  • 6
  • You have asked this question **two times before**, one of which was closed. I even commented on your previous question saying you'd asked it once before. Reposting multiple times won't get you anywhere. – Bojangles Dec 11 '11 at 23:23
  • @JamWaffles: Actually, those questions are asking how to *store* the image in the database. This one is asking how to *retrieve and display* the image from the database. Although, it is still a very poor question. – animuson Dec 11 '11 at 23:25
  • @animuson You are indeed correct. Either way, his previous question was asked twice and this one is very similar. On top of that, sabbir is unlikely to contribute to this community in any way other than posting poor questions. – Bojangles Dec 11 '11 at 23:27

1 Answers1

3

First off, Don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.


Try this:

<?php
$db=mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("image",$db) or die(mysql_error());

$id = (int)$_REQUEST['id'];

$result = mysql_query("SELECT * FROM image WHERE id=$id",$db);
if(mysql_numrows($result)>=1){
    $row = mysql_fetch_assoc($result);
    header("Content-type: image/jpeg");
    echo $row['image'];
}
?>

also

 echo "Image Uploaded. <p /> Your image <p /> <img src=get.php?id=$last_id>";

is not valid html.

 echo 'Image Uploaded. <p> Your image </p> <img src="get.php?id='.$last_id.'" />';
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • Sir, plz tell me what change should i make in " header("Content-type: image/jpeg"); " statement for pdf or other file format... – user2015 Dec 11 '11 at 23:38
  • 1
    `header('Content-type: application/pdf');`. Check the examples at http://php.net/manual/en/function.header.php – Cyclonecode Dec 11 '11 at 23:41