tags:

views:

64

answers:

1

In my Database, the images(jpeg,bmp format) are stored in bytea datatype, showing up in binary code in the database. Now I want to retrieve the images from the database. But i could not get the image in the web page. When i retrieve using this code given below, it shows the binary code value.(ie combination of numbers,characters,symbols). My code was

$dbconn = pg_connect("host=localhost user=xxxx password=xxxx dbname=xxxx")
 or die('Could not connect: ' .pg_last_error());

$rs = pg_query($dbconn, "select scan_image from image where cno='4' and imageno='1'");

$image = pg_escape_bytea(pg_fetch_result($rs, 0));

echo $image;

Am i correct with this code? Please help me to find the solution.

+6  A: 

Before you echo out the image content, you need to set the headers like:

header('Content-type: image/jpeg');

Then you can call your script in an image tag of the page where you want the fetched image to be displayed:

<img src="name_of_your_script.php">

This link will help you: Managing Images With a Web Database Application

codaddict
+1, just remember to use the right content-type for every type of image (png, gif, jpg, bmp)
DaNieL