How to fetch and show single image that’s path is store inside MySQL db and image uploaded on server.
In this tutorial we are showing image directly from MySQL database because the image path is stored into MySQL db. So here is the complete step by step tutorial for Display an image in PHP from MySQL database example .
Note: Please also read our Upload image in PHP & Store Image Name,Path into MySQL database tutorial.
How to Display an image in PHP from MySQL database example .
Code for show-image-from-db.php file.
<!doctype html> <html> <head> <meta charset="utf-8"> <title>Display an image in PHP from MySQL database example</title> </head> <body> <img src="<?php getimage(); ?>" width="auto" height="auto"> </body> </html> <?php function getimage() { include 'dbconfig.php'; $image = "SELECT * FROM image_upload WHERE id=8"; $image_info = mysql_query( $image, $conn ); while($image_data = mysql_fetch_array($image_info)) { echo "images/" . $image_data['image_name']; } } ?>
Code for dbconfig.php file.
<?php //Define your host here. $hostname = "localhost"; //Define your database username here. $username = "root"; //Define your database password here. $password = ""; //Define your database name here. $dbname = "test"; $conn = mysql_connect($hostname, $username, $password); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db($dbname, $conn); ?>
Screenshot :