Home > Database > Mysql Tutorial > How to Display MySQL Database Images in an HTML Image Tag Using PHP?

How to Display MySQL Database Images in an HTML Image Tag Using PHP?

DDD
Release: 2024-12-14 02:49:10
Original
645 people have browsed it

How to Display MySQL Database Images in an HTML Image Tag Using PHP?

Retrieving Images from MySQL Database and Displaying in an HTML Image Tag

Question:

In PHP, you've created a MySQL database with a table that includes a BLOB column for storing JPEG images. You've also written a PHP script, catalog.php, to retrieve the image from the database and display it in an HTML image tag. However, you're experiencing issues accessing the $result variable in your catalog.php script.

Answer:

Directly accessing the $result variable in HTML is not possible. To display the image in the HTML tag, you need to create a separate PHP script to fetch the image data from the database and return it as an image.

Updated catalog.php:

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

New PHP Script: getImage.php:

<?php

$link = mysqli_connect("localhost", "root", "", "dvddb");
$sql = "SELECT dvdimage FROM dvd WHERE>
Copy after login

How it Works:

  • The catalog.php script calls the getImage.php script with the id of the image you want to display.
  • getImage.php connects to the database, executes the SQL query to retrieve the image data, and stores it in the $image variable.
  • getImage.php sets the Content-type header to "image/jpeg" and echoes the $image variable, which sends the image data to the browser.
  • The browser receives the image data and displays it in the HTML image tag specified in catalog.php.

The above is the detailed content of How to Display MySQL Database Images in an HTML Image Tag Using PHP?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template