且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP:使用PDO从MySQL检索图像

更新时间:2023-02-22 23:27:10

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

You need to paramaterize the imageid value and bind the parameter to 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;

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

Of course, you'll also want to specify the complete, correct content type (e.g., image/png).