如何将存储在mySQL数据库中的图像显示为BLOB? 到目前为止它尝试了什么: 1.创建一个新的php函数/文件来获取图片(getpicture.php)。 2.在html中,我有以下代码:
<img src="getpicture.php?id=2" border ="0" height="250" width="250" />
/*below is the getpicture.php*/
<?php
@ $db = new MySQLi('localhost','root','','myDatabase');
if(mysqli_connect_errno()) {
echo 'Connection to database failed:'.mysqli_connect_error();
exit();
}
if(isset($_GET['People_Id'])) {
$id = mysqli_real_escape_string($_GET['People_Id']);
$query = mysqli_query("SELECT * FROM 'people' WHERE 'People_Id' = '$id'");
while($row = mysqli_fetch_assoc($query)) {
$imageData =$row['image'];
}
header("content-type: image/jpeg");
echo $imageData;
echo $id;
}
else {
echo "Error!";
echo $id;
}
?>
代码有什么问题?请帮忙!
答案 0 :(得分:1)
我回答了自己的问题,现在正在努力..
以下是getpicture.php:
<?php
$db = new MySQLi('localhost', '', '', 'mydatabase');
if ($db->connect_errno) {
echo 'Connection to database failed: '. $db->connect_error;
exit();
}
if (isset($_GET['id'])) {
$id = $db->real_escape_string($_GET['id']);
$query = "SELECT `Picture` FROM member WHERE `Id` = '$id'";
$result = $db->query($query);
while($row = mysqli_fetch_array($result)) {
$imageData = $row['Picture'];
header("Content-type:image/jpeg");
echo $imageData;
}
} ?&GT;
检索上面的getpicture.php的php脚本如下所示:
echo '<img src="getpicture.php?id=' . htmlspecialchars($_GET["id"]) . '"border ="0" height="250" width="250" />';
感谢大家的帮助
答案 1 :(得分:0)
这是错误的:
$query = mysqli_query("SELECT * FROM 'people' WHERE 'People_Id' = '$id'");
你对表名使用错误的引号(必须是反引号而不是单引号(请参阅tylda~key)。请参阅文档:http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
这也是错误的
header("content-type: image/jpeg");
echo $imageData;
echo $id;
删除上一个echo $i;
并将其替换为exit();
,否则会损坏刚发送的图像数据流。
答案 2 :(得分:0)
很多都错了。
这是一个更新的代码示例
<?php
$db = new MySQLi('localhost', 'user', 'password', 'myDatabase');
if ($db->connect_errno) {
echo 'Connection to database failed: '. $db->connect_error;
exit();
}
if (isset($_GET['id'])) {
$id = $db->real_escape_string($_GET['id']);
$result = $db->query("SELECT * FROM `people` WHERE `People_Id` = '$id'");
$row = $result->fetch_assoc();
$imageData = $row['image'];
header("Content-type: image/jpeg\n\n");
echo $imageData;
}
else {
echo "Error!";
}