从数据库

时间:2018-06-15 17:30:55

标签: php mysql base64

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

LzlqLzRBQVFTa1pKUmdBQkFRQUFBUUFCQUFELzJ3QkRBQkFMREE0TUNoQU9EUTRT ..etc

php code。

<!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>

任何解决方案如何显示db中的图像以解决当前问题?

1 个答案:

答案 0 :(得分:0)

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>