Home > Backend Development > PHP Tutorial > How to Display MySQL BLOB Images in an HTML `` Tag?

How to Display MySQL BLOB Images in an HTML `` Tag?

Mary-Kate Olsen
Release: 2024-12-18 05:55:09
Original
629 people have browsed it

How to Display MySQL BLOB Images in an HTML `` Tag?

Retrieving and Displaying Images from MySQL in an HTML Tag

Question:

You have created a MySQL database and a table with a BLOB column to store JPEG files. While attempting to retrieve the image data into an HTML tag, you're encountering issues with the PHP variable $result. How can you incorporate $result into the HTML to display the image?

Answer:

It is not possible to directly include the $result variable in the HTML tag. The $result variable contains the raw image data retrieved from the database. To display the image, you need to create a separate PHP script that will output the image data.

Solution:

  1. Modify catalog.php:

    • Change the HTML tag to refer to a new PHP script that will retrieve and output the image data:

      <img src="getImage.php?id=1" width="175" height="200" />
      Copy after login
  2. Create getImage.php:

    • Create a new PHP script named getImage.php that performs the following tasks:

      <?php
      
      // Establish database connection
      $link = mysqli_connect("localhost", "root", "", "dvddb");
      
      // Prepare SQL statement
      $sql = "SELECT dvdimage FROM dvd WHERE>
      Copy after login

This script establishes a database connection, executes the SQL statement to retrieve the image data for the specified ID, sets the image content type header, and outputs the image data.

Explanation:

By creating a separate script to retrieve and output the image data, you can separate the database operations from the HTML display. This approach ensures that your HTML code remains clean and organized.

The above is the detailed content of How to Display MySQL BLOB Images in an HTML `` Tag?. 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