How to retrieve images from MySQL database and display in an html tag

How to retrieve images from MySQL database and display in an html tag

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to retrieve images from MySQL database and display in an html tag

The core issue preventing your image from displaying lies in how you are currently accessing the data returned by mysql_query. In your existing code, $result is a resource representing the result set of your SQL query. When you execute SELECT dvdimage, the database returns a single BLOB (Binary Large Object). You cannot simply echo this resource directly into an HTML <img> tag; you must first extract the binary content from that resource and convert it into a format the browser understands, such as a Base64-encoded string or a direct data URI.

Furthermore, your current approach relies on the mysql_* extension, which is deprecated and removed in PHP 7.0 and later. If your environment runs on modern versions of PHP (which is highly recommended for security and compatibility), this code will fail immediately. You need to migrate to mysqli or PDO to handle database connections securely and retrieve binary data correctly.

Extracting the BLOB Content

To display the image, you first need to fetch the raw binary data from the result set. Using the deprecated mysql_fetch_array() with an associative flag is the most straightforward way to isolate the specific column you need. Once you have the binary string stored in a PHP variable, you can use base64_encode() to convert that binary blob into a text string. This is necessary because HTML attributes cannot accept raw binary data; they require a string representation.

Here is how you would update your catalog.php logic using the modern mysqli extension:

<?php
  // Establish connection and select database
  $link = new mysqli("localhost", "root", "", "dvddb");
  
  if ($link->connect_error) {
    die("Connection failed: " . $link->connect_error);
  }

  // Prepare the query
  $sql = "SELECT dvdimage FROM dvd WHERE id = ?";
  $stmt = $link->prepare($sql);
  
  // Bind the parameter (id=1 is an integer)
  $stmt->bind_param("i", $id);
  $id = 1;
  
  $stmt->execute();
  $result = $stmt->get_result();

  if ($row = $result->fetch_assoc()) {
    // Fetch the binary image data
    $imageData = $row['dvdimage'];

    // Convert binary blob to Base64 string for HTML usage
    $encodedImage = base64_encode($imageData);

    // Construct the full data URI
    // Assuming JPEG based on your source material, use 'image/jpeg'
    echo "<img src='data:image/jpeg;base64," . $encodedImage . "' width=\"175\" height=\"200\" />";
  } else {
    echo "No image found for this ID.";
  }

  $link->close();
?>

Why Base64 Encoding?

You might wonder why we aren't just setting the src attribute to a file path like /images/dvd.jpg. Since your data resides in a MySQL BLOB column, there is no physical file on your server's filesystem associated with that database record. The image exists only as bytes within the database schema. To render it in the browser without creating separate files on the disk, you must embed the image data directly into the HTML stream. Base64 encoding transforms those binary bytes into a safe, ASCII-compatible string that can be placed inside the src attribute.

Trade-offs and Performance Considerations

While embedding images via Base64 is convenient for small galleries or specific single-view pages, it has significant drawbacks for large-scale applications. The primary concern is payload size. Every time a user visits the page, the entire image data travels over the network from the database to the browser, encoded as text rather than binary. This increases bandwidth usage and slows down page load times compared to serving images from a local cache or CDN.

If you are building a public-facing catalog with many images, storing BLOBs directly in the database is often an anti-pattern. A more robust architecture involves moving the image files to a dedicated storage solution (like AWS S3 or a local /public/uploads directory) and storing only the filename and path in the dvdimage column. This separates your read-heavy media assets from your transactional database, improving performance and making backup strategies simpler.

Conclusion on Implementation

To successfully display images stored as BLOBs in MySQL, you must fetch the binary data, encode it to Base64, and inject it into a data:image/... URI within your HTML tag. Ensure you are using a modern database extension like mysqli or PDO to avoid deprecation warnings and security vulnerabilities inherent in the legacy mysql_* functions.

Our network