1

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.

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>

any solution how do display the images from db for my current problem?

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

Decode your image string in 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>
F5 Buddy
  • 474
  • 4
  • 4