【发布时间】:2015-08-30 15:21:56
【问题描述】:
我正在尝试显示来自 mysql 数据库的图片,但不知何故它不起作用。图片为jpeg格式。我使用带有数据库连接文件和源文件的简单 php 代码。奇怪的是,如果我想直接从数据库中下载图片,它会保留为二进制 .bin 文件。这是我的代码:
db_connect.php:
<?php
$dbh = new PDO('mysql:host=localhost;dbname=probe', $root);
?>
source.php:
<?php
include ‘db_connect.php’;
$query = “select * from users”;
$stmt = $con->prepare( $query );t
$stmt->bindParam($_GET['image']);
$stmt->execute();
$num = $stmt->rowCount();
if( $num ){
$row = $stmt->fetch(PDO::FETCH_ASSOC);
header(“Content-type: image/jpeg”);
print $row['image'];
exit;
}else{
//if no image found with the given id,
//load/query your default image here
}
?>
而在索引文件中我只是调用source.php来显示图片:
<img src="../reg/source.php" >
这是 SQL 源代码:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` blob NOT NULL,
`image_type` varchar(100) NOT NULL,
`image_size` varchar(100) NOT NULL,
`image_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=44 ;
【问题讨论】:
-
不清楚这些花括号
‘ ’和“ ”是否是您实际代码的一部分,包括;t中的“t” -
这是我的错。我应该更仔细地查看它并发布正确编码的版本。对不起!
-
在进行了更多挖掘之后,您还缺少一些
WHERE id = ?以插入到select * from users中作为示例,因为您没有绑定任何东西。您可能已经在 codeofaninja.com/2011/02/… 找到了该代码 - 这是另一个答案 stackoverflow.com/a/22352477 的示例。