Home > Article > Backend Development > How to Display BLOB Images from MySQL in PHP?
How to Display BLOB Images from MySQL Database in PHP
You're encountering issues displaying a BLOB image on your PHP page. Let's explore the solutions:
Option 1: Inline Base64 Encoding
This method is suitable for displaying a few images. Inline base64 encoding converts the binary image data into a data: URI scheme. The HTML for this would be:
<img src="data:image/jpeg;base64,<?php echo base64_encode($image); ?>" />
Option 2: Create an Image PHP File
This method is preferred for handling numerous images. You'll create an "image.php" file to retrieve the image from the database and output it. The HTML would be:
<img src="image.php?id=<?php echo $image_id; ?>" />
And the PHP page would look like:
<?php $id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0; $image = getImageFromDatabase($id); // Retrieve image from database header('Content-Type: image/jpeg'); echo $image; ?>
In your PHP code, you can fetch the image data from the database using a MySQL query or a PHP data abstraction layer (e.g., PDO). Ensure that you format the HTML and PHP code correctly, including proper header and MIME type specifications.
The above is the detailed content of How to Display BLOB Images from MySQL in PHP?. For more information, please follow other related articles on the PHP Chinese website!