0

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
  • We do not email answers. We post them under your question, as then other people can see them as well. Commented Feb 8, 2010 at 5:55

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.