Home  >  Article  >  Backend Development  >  How Can I Display BLOB Images from MySQL in PHP?

How Can I Display BLOB Images from MySQL in PHP?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-17 07:27:03491browse

How Can I Display BLOB Images from MySQL in PHP?

Displaying BLOB Images from MySQL Database in PHP Page

You have encountered an issue displaying BLOB images from a MySQL database in your PHP page. Specifically, you're experiencing difficulty with interpreting binary data as an image.

In PHP, handling BLOB data requires specific steps to retrieve and display the binary as an image. Here are two approaches you can attempt:

1. Inline Base64 Encoding

Base64 encoding converts binary data into a text format. This approach outputs an image URL with encoded image data:

echo '<dt><strong>Technician Image:</strong></dt><dd>' . '<img src="data:image/jpeg;base64,' . base64_encode($row2['image']) . '" width="290" height="290"></dd>';

2. Image Retrieval PHP File

This involves creating a dedicated PHP file that retrieves the image from the database based on an ID parameter. Your HTML will reference this file, which then outputs the image content:

<img src="image.php?id=<?php echo $image_id; ?>">
// image.php
$id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
$image = getImageFromDatabase($id); // Retrieves image from database

header('Content-Type: image/jpeg');
echo $image;

The above is the detailed content of How Can I Display BLOB Images from MySQL in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn