为什么mysql数据库中的图像显示为已损坏?

时间:2017-08-18 12:34:28

标签: php mysql image blob

我在mysql数据库中将图像存储为blob数据类型,但是当检索到图像时,它显示为图像损坏。我不确定以下代码有什么问题:

<?php
$msg = '';
if (isset($_POST['upload'])){
    $image = $_FILES['image']['tmp_name'];
    $img = file_get_contents($image);
    $con = mysqli_connect('localhost','root','','mydatabase') or die('Unable To connect');
    $sql = "insert into info (image) values(?)";

    $stmt = mysqli_prepare($con,$sql);

    mysqli_stmt_bind_param($stmt, "s",$img);
    mysqli_stmt_execute($stmt);

    $affected = mysqli_stmt_affected_rows($stmt);
    if($affected==1){
        $msg = 'Done';
    }else{
        $msg = 'Failed';
    }
        mysqli_close($con);
}
if (isset($_POST['show'])){
$id=2;
$con= mysqli_connect("localhost","root","","mydatabase");
$sql = "SELECT * FROM info WHERE id = $id";
$sth = $con->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
mysqli_close($con);
}
    ?>

1 个答案:

答案 0 :(得分:0)

我发现了错误。刚刚将数据库中的图像数据类型更改为LONGBLOB而不是BLOB。