简体   繁体   中英

How to get BLOB image from MySQL

I want to display a BLOB image from my MySQL db.

I already tried these solutions but I couldn't figure out how to use them in my code: Stack post

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM data ORDER BY data.id desc";
$result = $conn->query($sql) or die(mysqli_error($conn));//get query code error
$rows = $result->fetch_all(MYSQLI_ASSOC); //use fetch_all()
echo "<table class='table table-striped table-bordered'>
<tr>
<th>ID</th>
<th>date</th>
<th>security</th>
<th>photo</th>
</tr>"; // put table code outside

if ($result->num_rows > 0) {
    foreach($rows as $row) { //apply foreach()
        echo "<tr>
        <td>" . $row["id"]. "</td>
        <td>" . $row["Date"]."</td>
        <td>" . $row["Security"]. "</td>
        <td>" . $row["Photo"]. "</td>
    </tr>"; 
    }
} else {
    echo "No data found";
}
echo "</table>"; //close table outside
$conn->close();
?>

Can someone explain how to make it work? Or repost my code with the solution in it. I tried to insert this in my code but couldn't figure it out:

"<img src='data:image/jpeg;base64,' . base64_encode( $row['Photo'] ) . '' />";

We upload JPG files.

instead of

<td>" . $row["Photo"]. "</td>

Please use this

<td><img src='data:image/jpeg;base64," . base64_encode($row['Photo']) . "' /></td>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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