Hello i am trying to retrieve image stored in database as BLOB however it is coming back as broken so the image doesnt display what am i doing wrong code below.
<?php
$connect = mysqli_connect("localhost", "root", "", "testing");
$query = "SELECT * FROM item ORDER BY item_id DESC";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result)){
echo '<tr>';
echo '<td>';
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['photo'] ).'" height="100" width="100" class="img-thumnail" />';
echo '</td>';
echo '</tr>';
}
?>
Use addslashes
function while save BLOB image into database. Because apostrophe '
may broken your image, addslashes function replace '
with \'
which solve the problem.
For Inserting BLOB image into DataBase
$connect = mysqli_connect("localhost", "root", "", "testing");
$image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
$query = "INSERT INTO item (item_id,photo) VALUES('','$image')";
$result = mysqli_query($connect, $query);
For Accessing BLOB image From DB
$connect = mysqli_connect("localhost", "root", "", "testing");
$query = "SELECT * FROM item ORDER BY item_id DESC";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result)){
echo '<tr>';
echo '<td>';
echo '<img src="data:image/jpeg;base64,'.base64_encode($row['photo'] ).'" height="100" width="100" class="img-thumnail" />';
echo '</td>';
echo '</tr>';
}