How do i display images stored as blob data type in mysql with php? [closed]

穿精又带淫゛_ 提交于 2019-12-12 06:48:40

问题


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!


回答1:


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




回答2:


This is wrong:

$query = mysqli_query("SELECT * FROM 'people' WHERE 'People_Id' = '$id'");

you use wrong quotes for table name (must be backtick instead of single quote (see tylda ~ key). See docs: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

Also this is wrong too

header("content-type: image/jpeg");
echo $imageData;
echo $id;

get rid of last echo $i; and replace it with exit(); otherwise you corrupt the image data stream you just sent.




回答3:


Plenty is wrong.

  1. your SQL is wrong. Remove the single quotes from the table and column and replace with back ticks.
  2. Although it may still work, you should have a couple of new lines after your header
  3. You shouldn't echo out your $id after you echo your image data.
  4. You're checking for the wrong value when you check isset
  5. Also, you should be using OOP for mysqli
  6. Since your image data is only a single row, you don't need to wrap it in a while loop

Here is an updated code example

<?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!";
}


来源:https://stackoverflow.com/questions/25209913/how-do-i-display-images-stored-as-blob-data-type-in-mysql-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!