Storing and retrieving images as binary large objects (BLOBs) in a MySQL database is a common technique. However, displaying these images can sometimes be challenging.
Inserting an Image:
$query = "INSERT INTO products (image) VALUES(?)"; $stmt = $db->prepare($query); $stmt->bind_param('s', $image); $stmt->execute();
Displaying an Image:
$query = "SELECT * FROM products WHERE id = ?"; $stmt = $db->prepare($query); $stmt->bind_param('s', $id); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_array();
echo '<img src="data:image/jpeg;base64,' . base64_encode($row['image']) . '"/>';
The above is the detailed content of How to Display MySQL BLOB Images in PHP?. For more information, please follow other related articles on the PHP Chinese website!