Displaying Images from a Database in PHP: A Resolution
Displaying images from a database can be a daunting task for beginners, but it can be accomplished with ease. Here's a solution to the question raised:
The provided PHP code attempts to display an image from a database, but it merely prints the path to the image as "user-1.jpg." To correctly display the image, we need to fetch its binary contents from the database and embed them into the web page.
Here's an updated version of the code:
$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']) . '" />';
Explanation:
By implementing this solution, you should be able to successfully display images from your database in your PHP web applications.
The above is the detailed content of How to Display Images Stored in a Database Using PHP?. For more information, please follow other related articles on the PHP Chinese website!