2010-02-08 36 views
0

我在PHP中使用Postgresql作为后端。数据库已由其他人设计。现在我想使用PHP从数据库中检索图像。但我无法从数据库中检索图像。我使用此代码来检索图像,但它只显示网页中不可读的字符。有时它会显示一个小图标(就像我们在网页上没有任何图像时),但大多数情况下它会显示一个空白页面。存储图像的数据类型是“bytea”。我用来显示图像的代码如下:使用PHP从postgres数据库检索图像

$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)); 
    } 
} 
+0

我们不会通过电子邮件发送答案。我们将它们发布在您的问题下,因为其他人也可以看到它们。 – 2010-02-08 05:55:51

回答

1

您没有看到您的结果属性。

  1. pg_result()不是由PostgreSQL扩展定义的函数。
  2. 您不会将pg_fetch_array()的返回值存储在变量中。

以下代码应该向浏览器输出scan_image值。

$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']; 
} 

我不幸的是不知道scan_image的文件格式。不要忘记发送正确的标题。