Display multiple images from MySQL database in PHP

How to create php script that’s show multiple images one by one from MySQL db.

In this tutorial we are creating an PHP code to display all the images which paths are stored into MySQL database. Using this script it will call the images name which stored inside a pre-created folder and fetch them and set them into image tag one by one. So here is the complete step by step tutorial for Display multiple images from MySQL database in PHP.

android-project-download-code-button

How to Display multiple images from MySQL database in PHP.

Code for display-all-images.php file.

 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Display multiple images from MySQL database in PHP</title>
</head>

<body>

<?php

include 'dbconfig.php';

$image_query = mysql_query("SELECT * FROM image_upload") or die(mysql_error());

$file_path = 'http://localhost/home/images/';

while($all_images = mysql_fetch_assoc( $image_query ))

{
 
$img_src = $file_path.$all_images['image_name'];

echo "<center><img src=".$img_src."> </center> </br>";

}
?>

</body>
</html>

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:

Display multiple images from MySQL database in PHP

Click here to download Display multiple images from MySQL database in PHP project with MySQL database included.

Leave a Reply

Your email address will not be published. Required fields are marked *