2010-02-25 86 views
2

在我的数据库中,图像(jpeg,bmp格式)存储在bytea数据类型中,以数据库中的二进制代码显示。现在我想从数据库中检索图像。但我无法在网页中获取图像。当我使用下面给出的代码进行检索时,它显示了二进制代码值(即数字,字符,符号的组合)。我的代码是如何从数据库中检索图像?

$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; 

我是否正确使用此代码?请帮我找到解决方案。

回答

6

你回声出图像内容之前,您需要设置标题,如:

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

然后,你可以打电话给你的脚本要显示的获取图像的页面的图像标签:

<img src="name_of_your_script.php"> 

此链接将帮助您:Managing Images With a Web Database Application

+0

+1,只记得使用正确的内容类型,为每个类型的图像(PNG,GIF,JPG,BMP) – Strae 2010-02-25 07:42:09

相关问题