php - 从数据库中检索 base64 图像

标签 php mysql base64

我创建了一个简单的表格,并用 base64 将图像存储为文本, 当我尝试从数据库中获取所有数据时,它运行良好,除了图像像下面的代码一样显示在网站上。

LzlqLzRBQVFTa1pKUmdBQkFRQUFBUUFCQUFELzJ3QkRBQkFMREE0TUNoQU9EUTRT ..etc

PHP 代码。

<!DOCTYPE html>
<html>

<head>
    <meta charset = "utf-8">
    <title> Welcome </title>
    <script> </script>
    <style></style>
</head>
    <body>
        <table border = "solid">
            <tr>
                <th> id </th>
                <th> Millitary number </th>
                <th> Fname </th>
                <th> Lname </th>
                <th> Image </th>
            </tr>
            <?php 
                $conn = mysqli_connect("localhost" , "id2549459_salamnti" , "0000000000" , "id2549459_tutorial");
                if($conn -> connect_error){
                    die("my connection faild" . $conn -> connect_error);
                }

                $sql = "SELECT id, militry_num , firstname, lastname,image from students";
                $result = $conn-> query($sql);
                if($result -> num_rows > 0){
                    while($row = $result-> fetch_assoc()){
                        echo "<tr> <td>" . $row["id"] .  "</td> <td>"
                            . $row["militry_num"] . 
                             "</td> <td>". $row["firstname"] .
                             "</td><td>" . $row["lastname"] . "</td><td>" .
                             base64_encode($row["image"]) . 
                             "</td> </tr>";
                }
                    echo "</table>";
                }
                else{
                echo "0 result" . $conn->error;
                    }
                        $conn -> close();
            ?>
        </table>
    </body>
</html>

任何解决方案如何显示来自数据库的图像以解决我当前的问题?

最佳答案

data:image/gif;base64 中解码您的图像字符串

 <img src="data:image/gif;base64,' . $row["image"] . '" />
<head>
<meta charset = "utf-8">
<title> Welcome </title>
<script> </script>
<style></style>
</head>
<body>
    <table border = "solid">
        <tr>
            <th> id </th>
            <th> Millitary number </th>
            <th> Fname </th>
            <th> Lname </th>
            <th> Image </th>
        </tr>
        <?php 
           $conn = mysqli_connect("localhost" , "id2549459_salamnti" , "0000000000" , "id2549459_tutorial");
            if($conn->connect_error){
                die("my connection faild" . $conn->connect_error);
            }

            $sql = "SELECT id, militry_num , firstname, lastname,image from students";
            $result = $conn->query($sql);
            if($result->num_rows > 0){
                while($row = $result->fetch_assoc()){
                    echo "<tr> <td>" . $row["id"] .  "</td> <td>"
                        . $row["militry_num"] . 
                         "</td> <td>". $row["firstname"] .
                         "</td><td>" . $row["lastname"] . "</td><td>" .
                         '<img src="data:image/gif;base64,' . $row["image"] . '" />' . 
                         "</td> </tr>";
            }
                echo "</table>";
            }
            else{
            echo "0 result" . $conn->error;
                }
              $conn->close();
        ?>
    </table>
</body>

关于php - 从数据库中检索 base64 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50880279/

相关文章:

php - 我怎样才能用数据库中的数据制作这个数组?

php - 使用 PHP 将 Base64 字符串作为二进制文件保存到磁盘

Javascript - 如何将图像缩小到特定文件大小?

JavaScript/PHP : Alert dialog after successfully saved

php - 检查 PHP session_id 是否被使用

mysql - 除了初始成本之外,使用 MySQL 而不是 SQL Server 和 .net 有什么其他好处吗?

mysql db records visibility (design and select help)

Objective-C Base64 解码返回 nil

php - 将修改后的预序树遍历模型(嵌套集)获取到 <ul>

javascript - 如何在服务器部分反序列化 JSON 数据