Displaying Images from Database Using PHP
In PHP, displaying images stored in a database is a common task. Let's explore a code snippet to accomplish this:
$db = mysqli_connect("localhost","root","","DbName"); $sql = "SELECT * FROM products WHERE id = $id"; $sth = $db->query($sql); $result = mysqli_fetch_array($sth); echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
This code utilizes the MySQL database extension (mysqli) to retrieve image data from a database and then employs base64 encoding to display the image as an inline HTML element. Here's what each section of the code does:
By implementing the provided code, you can successfully display images stored in a database on your web page.
The above is the detailed content of How to Display Images from a Database Using PHP?. For more information, please follow other related articles on the PHP Chinese website!