简体   繁体   English

从数据库检索base64图像

[英]retrieve base64 images from database

I have created simple table and stored an images with base64 as a TEXT , when I tried to fetch all data's from database, it works well, Except the images is displaying on website like below code. 我创建了一个简单的表并使用base64作为TEXT存储了一个图像,当我尝试从数据库中获取所有数据时,它运行良好,除了图像如以下代码一样显示在网站上。

LzlqLzRBQVFTa1pKUmdBQkFRQUFBUUFCQUFELzJ3QkRBQkFMREE0TUNoQU9EUTRT ..etc

php code. 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>

any solution how do display the images from db for my current problem? 任何解决方案如何针对我当前的问题显示来自db的图像?

Decode your image string in data:image/gif;base64 将您的图片字符串解码为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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM