【问题标题】:Echoing image from MySQL Database PHP来自 MySQL 数据库 PHP 的回显图像
【发布时间】:2016-08-11 18:07:31
【问题描述】:

我一直在尝试使用 PHP 从我的数据库中回显图像。我在论坛上尝试了各种解决方案,但都没有成功。我只是得到一个空白图像作为输出。

while($rows=mysqli_fetch_assoc($getquery))
{
    $id=$rows['id'];
    $LastName=$rows['LastName'];
    $FirstName=$rows['FirstName'];
    $Year=$rows['Year'];
    $Press=$rows['Press'];
    $Description=$rows['Description'];
    $Title=$rows['Title'];
    $image=$rows['image'];


    echo '<div class = "paragraph">' . $LastName . '<br/>'. $FirstName . '<br/>' . $Year . '<br/>' .  $Press . '<br/>' . $Title . '<br/>'. $Description . "<img src='image/".$row['image']."' />" . '</div>' ;
}

【问题讨论】:

标签: php mysql image echo


【解决方案1】:

您将图像回显为$row['image'],但之前对图像的唯一引用是$rows['image'](注意s)和$image。更新 echo 语句以使用其中任何一个而不是 $row['image']

编辑:这不会完全解决您的问题。正如对您问题的评论中所述,您需要进行一些操作才能实际显示图像,如here 所示。

【讨论】:

    【解决方案2】:
    while($rows=mysqli_fetch_assoc($getquery)){
        $id=$rows['id'];
    
        /*
           assuming the `image` directory is relative to the root
           and not the directory from which the script runs then
           a leading slash might be the issue.
        */
        echo "
        <div id='{$id}' class='paragraph'>
            {$rows['LastName']}<br/>
            {$rows['FirstName']}<br/>
            {$rows['Year']}<br/>
            {$rows['Press']}<br/>
            {$rows['Title']}<br/>
            {$rows['Description']}
            <!--<img src='/image/{$row['image']}' />-->
            <img src='data:image/jpeg;base64, {$rows['image']}' alt='' title='' />
        </div>";
    }
    

    如果您在将图像保存到数据库时存储图像的 mimetype,那么您将在上面替换正确的 mime/content 类型 ~ 即:image/pngimage/gif 等,这样可能会变成这样:

    <img src='data:{$rows['mimetype']};base64, {$rows['image']}' alt='' title='' />
    

    【讨论】:

    • 图像甚至不在目录中,它们在数据库中。我需要他们从数据库中回显。
    • 所以你已经存储了base64编码的图像内容并且需要输出?通常格式类似于&lt;img src="data:image/jpeg;base64, /&lt;BASE64_STRING&gt;" /&gt;
    • 是的,这基本上就是我所需要的。我现在正在寻找一种方法来实现这一点。
    猜你喜欢
    • 1970-01-01
    • 2020-05-19
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2017-07-15
    • 2014-04-08
    • 2012-02-03
    相关资源
    最近更新 更多