Home > Database > Mysql Tutorial > How to Display Images Stored as BLOBs in MySQL?

How to Display Images Stored as BLOBs in MySQL?

Patricia Arquette
Release: 2024-12-05 06:34:10
Original
237 people have browsed it

How to Display Images Stored as BLOBs in MySQL?

Retrieve and Display Images from MySQL BLOB Field

You're facing the challenge of displaying an image stored as a BLOB field in your MySQL database. The error you're encountering suggests that you haven't correctly specified the MIME type of the image.

To resolve this issue, you can use one of two approaches:

Option 1: Inline Base64 Encoding

Encode the image as a base64 string and embed it directly into your HTML. This is not recommended for large numbers of images.

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

Option 2: Generate Image PHP File

Create a separate PHP file that takes the image ID as a query string parameter and outputs the image.

<img src="image.php?id=<?php echo $image_id; ?>" />
Copy after login
$id = (isset($_GET['id']) &amp;&amp; is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
$image = getImageFromDatabase($id); // your code to fetch the image

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

Ensure that you specify the correct MIME type with header('Content-Type: image/jpeg') before echoing the image. This will instruct the browser to interpret the data as an image.

The above is the detailed content of How to Display Images Stored as BLOBs in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template