PHP:使用PDO从MySQL检索图像

PHP:使用PDO从MySQL检索图像,php,mysql,image,pdo,Php,Mysql,Image,Pdo,我正在重构一些旧代码,包括重写基本的mysql查询以使用PDO 以下内容适用于所有浏览器和所有图像类型: $query = 'SELECT image FROM image WHERE imageid=' . $image_id; $result = mysql_query($query, $db_conn); querycheck($result); header("Content-type: image"); echo mysql_result($result, 0); 不幸的是,无论我如

我正在重构一些旧代码,包括重写基本的mysql查询以使用PDO

以下内容适用于所有浏览器和所有图像类型:

$query = 'SELECT image FROM image WHERE imageid=' . $image_id;
$result = mysql_query($query, $db_conn); querycheck($result);
header("Content-type: image");
echo mysql_result($result, 0);
不幸的是,无论我如何使用PDO重写它,它都不起作用。我已经浏览了整个PDO文档和标准的web搜索,但是没有一个建议/解决方案有效

如何使用PDO轻松地从MySQL获取和显示图像

编辑1:

马修·拉兹洛夫(Matthew Ratzloff)给出了下面应该是显而易见的答案,但它不起作用。 以下是我使用PDO测试的实际代码(我已经尝试了许多变体/参数):


我保留了相同的语法,尽管对于最终代码,$image\u id需要作为参数传递。上面的代码不起作用。PDO适用于所有类型的所有其他查询。

您需要对imageid值进行参数化,并将参数绑定到
PDO::PARAM_LOB

$sql = "SELECT image FROM image WHERE imageid=:id";
$query = $db_conn->prepare($sql);
$query->execute(array(':id' => $image_id));

$query->bindColumn(1, $image, PDO::PARAM_LOB);
$query->fetch(PDO::FETCH_BOUND);
header("Content-Type: image");
echo $image;

当然,您还需要指定完整、正确的内容类型(例如,image/png)。

您可以使用以下代码使用PDO从数据库获取图像:

public function getImage($id){
    $sql = "SELECT * FROM images WHERE id = ?";
    $sth = $this->dbh->prepare($sql);

    $sth->bindParam(1,$id);
    $sth->execute();


    $num = $sth->rowCount();
    if( $num ){
        $row = $sth->fetch(PDO::FETCH_ASSOC);
        header("Content-type: ".$row['type']);
        print $row['image'];
        exit;
    }else{
        return null;
    }
}
type-数据类型(列名)
如image/png或image/gif
image-图像数据(列名)
作为LOB存储在表中
$this->dbh
连接处理程序

它对我来说很有用,但现在我需要了解如何在JS中使用它,因为这个函数的结果被传递到JavaScript代码-所谓的ajax

这段代码显示dabase中的所有图像,以便您可以更改它并使它执行您希望它执行的操作

getMessage(); } ?>
我个人处理图像blob的方法是使用php文件提供图像,并结合GET参数。。。这是100%有效,它不涉及文件创建。。。例如,为blob中的图像提供服务的文件将是:

image.php:

<?php
$db = new PDO('mysql:host=localhost;dbname=anything; charset=utf8', 'account','password');
if (isset($_GET['imageid'])) {
    $result = $db->prepare("SELECT image FROM image where imageid = ?");
    if ($result->execute(array($_GET['imageid']))) {
        $row=$result->fetch();
        echo $row['pic']; //this prints the image data, transforming the image.php to an image
        }
    } // you can put an "else" here to do something on error...
?>

这可以从主脚本调用。。。例如:

<?php
$db = new PDO('mysql:host=localhost;dbname=anything; charset=utf8', 'account','password');
//... some code to do your job, where you get your imageid from
$imageid=...
?>
<img src="./image.php?imageid=<?php echo $imageid;?>">

">

您为PDO代码重新编写的代码可能会更有帮助。我发布了我测试过的答案,然后您将只进行修改。虽然这应该可以工作,但您不需要为MySQL使用LOB模式。这对于其他数据库来说是必要的。明确无误。:-)现在可以工作了。我只能假设这是早期版本的问题PHP/PDO。希望我能投票……更不用说它根本没有回答问题,与问题无关。
<?php
$db = new PDO('mysql:host=localhost;dbname=anything; charset=utf8', 'account','password');
if (isset($_GET['imageid'])) {
    $result = $db->prepare("SELECT image FROM image where imageid = ?");
    if ($result->execute(array($_GET['imageid']))) {
        $row=$result->fetch();
        echo $row['pic']; //this prints the image data, transforming the image.php to an image
        }
    } // you can put an "else" here to do something on error...
?>
<?php
$db = new PDO('mysql:host=localhost;dbname=anything; charset=utf8', 'account','password');
//... some code to do your job, where you get your imageid from
$imageid=...
?>
<img src="./image.php?imageid=<?php echo $imageid;?>">