views:

32

answers:

1

I am working in PHP with Postgresql as backend. The database is already designed by others. Now I want to retrieve image from the database using PHP. But I'm not able to retrieve image from the database. I using this code to retrieve image but it shows only unreadable characters in the webpage. Sometimes it shows a small icon (like when we get any image is not present in the webpage) but mostly it shows an empty page. The datatype from storing images is "bytea". The code I use to display the image is as follow:

$conn = pg_connect("user=xxxx password=xxxx dbname=xxxx host=xxxx");
$res = pg_query($conn, "SELECT scan_image FROM t1scandoc where image_srno='1'");
while(pg_fetch_array($res))
{
  if (pg_result($res,2))
  {
    WriteImageToFile(pg_result($res,0),$dir.pg_result($res,1),pg_result($res,2));
  }
}
+1  A: 

You are not reading your result property.

  1. pg_result() is not a function defined by the PostgreSQL Extension.
  2. You are not storing the return value of pg_fetch_array() in a variable.

The following code should output the scan_image value to the browser.

$conn = pg_connect("user=xxxx password=xxxx dbname=xxxx host=xxxx");
$res = pg_query($conn, "SELECT scan_image FROM t1scandoc where image_srno='1'");
if($row = pg_fetch_array($res)) // Output only one row, we can't output multiple
{
  echo $row['scan_image'];
}

I unfortunately do not know the file format of your scan_image. Don't forget to send the proper headers.

Andrew Moore