从数据库显示图像的破碎图像

时间:2017-03-30 18:28:51

标签: php html database blob

您好我正在尝试将存储在数据库中的图像作为BLOB检索,但是它会以损坏状态返回,因此图像不显示我在下面做错代码。

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

1 个答案:

答案 0 :(得分:0)

将BLOB映像保存到数据库中时使用addslashes功能。因为撇号'可能会破坏您的图像,所以addslashes函数会将'替换为\'来解决问题。

用于将BLOB图像插入数据库

$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);

用于从DB访问BLOB图像

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