我如何显示图像存储为MySQL中的blob数据类型与PHP? [英] How do i display images stored as blob data type in mysql with php?

查看:555
本文介绍了我如何显示图像存储为MySQL中的blob数据类型与PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将存储在mySQL数据库中的图像显示为BLOB?
到目前为止所尝试的是:
1.创建一个新的php函数/文件来获取图片(getpicture.php)。
2.在html中,我有以下代码:

 < img src =getpicture.php? id = 2border =0height =250width =250/> 




/ *下面是getpicture.php * /
<?php

@ $ db =新的MySQLi('localhost','root','','myDatabase');

if(mysqli_connect_errno()){
echo'连接数据库失败:'。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 {
echoError!;
echo $ id;
}

?>

代码有什么问题?请帮忙!

解决方案

b

以下是getpicture.php:

 <?php 

$ db = new MySQLi('localhost','','','mydatabase');

if($ db-> connect_errno){
echo'连接数据库失败:'。 $ 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;
}

}
?>



检索上面的getpicture.php的php脚本如下所示:

  echo'< img src =getpicture.php?id ='。htmlspecialchars($ _ GET [id])。'border =0height =250width =250/>'; 

为您提供帮助

How do i display an image stored in mySQL database as BLOB ? What it tried so far: 1. Created a new php function/file to get picture (getpicture.php). 2. In the html, I have the following code:

<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;
}

?>

What's wrong with the codes ? Please help!

解决方案

I answered my own question, it's working now..

Below is the 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;
}

} ?>

The php script which retrieve the getpicture.php above looks like this:

echo '<img src="getpicture.php?id=' . htmlspecialchars($_GET["id"]) . '"border ="0" height="250" width="250" />';

Thaank you all for the help

这篇关于我如何显示图像存储为MySQL中的blob数据类型与PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆