2015-01-21 129 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);