title: Retrieve Image from postgres database using PHP
body: <p>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:</p>
  
  <pre><code>$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));
    }
  }
  </code></pre>