2010-02-25 89 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

相關問題