2015-01-21 133 views
-1

我想在數據庫中插入圖像,但由於某些原因,我無法檢索它。我在數據庫中使用BLOB類型的圖像。PDO在數據庫中插入圖像並檢索圖像

我正在使用上傳圖片的代碼。有一次,我保存圖像數據庫,在「形象」專欄中,我得到我使用的打印網頁圖像文件圖像01.bin

if (isset($_POST["submit"]) { 

    $id= $_GET["id"]; 
    $image = $_POST["image"]; 

    $stmt=$conn->prepare("INSERT INTO db (id, image) VALUES (:id, :image)"); 

    $stmt->bindParam(:id, $id); 
    $stmt->bindParam(:image, $image); 
    $stmt->execute(); 
} 

代碼。

$stmt = "Select Id, Image from db where id = $id LIMIT 0,1" 

$q = $conn->query($stmt); 

while ($r = $q ->fetch(): 
    echo "<img src ='",$r[Image],"' width='100' height='100' />"; 
endwhile; 
+1

二進制圖像數據是「in」$ _POST ['image']?可能的,但是......你是怎麼做到的? – VolkerK 2015-01-21 09:02:01

+0

你必須閱讀的圖像內容把它放在一個blob ...看看http://stackoverflow.com/questions/23854070/pdo-insert-image-into-database-directly-always-inserting-blob-0b – 2015-01-21 09:02:01

+0

'echo「」;'在這個php文件中加入適當的mime類型 – 2015-01-21 09:03:02

回答

-1

您可以綁定您的類似於例1中的LOB類型選擇如下所示:http://php.net/manual/en/pdo.lobs.php

這裏的例子 - 注意,結合所選列,並將其設置爲PDO::PARAM_LOB

<?php 
$db = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2'); 
$stmt = $db->prepare("select contenttype, imagedata from images where id=?"); 
$stmt->execute(array($_GET['id'])); 
$stmt->bindColumn(1, $type, PDO::PARAM_STR, 256); 
$stmt->bindColumn(2, $lob, PDO::PARAM_LOB); 
$stmt->fetch(PDO::FETCH_BOUND); 

header("Content-Type: $type"); 
fpassthru($lob); 
相關問題