How Do I Display Images Stored As Blob Data Type In Mysql With Php?
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
Solution 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
Solution 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.
Solution 3:
Plenty is wrong.
- your SQL is wrong. Remove the single quotes from the table and column and replace with back ticks.
- Although it may still work, you should have a couple of new lines after your header
- You shouldn't echo out your $id after you echo your image data.
- You're checking for the wrong value when you check isset
- Also, you should be using OOP for mysqli
- 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!";
}
Post a Comment for "How Do I Display Images Stored As Blob Data Type In Mysql With Php?"