How to fetch image from database in php : In this article, you going to see inserting and display of image in detail explained in simple way.

Lets start with the topic of discussion regarding : How to fetch image from database in php

Before start with the discussion regarding fetching of image.

First create table name called image in the database.

Below is the SQL query execute in phpmyadmin so table is created.

---- Table structure for table `image`--CREATE TABLE `image` ( `id` int(11) NOT NULL, `title` varchar(500) NOT NULL, `imagename` text NOT NULL, `type` varchar(500) NOT NULL, `time` datetime NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

After executing the above Sql query table is created as shown below.

Created Table Image

Now create the configuration file to configure database connection file name config.php

<?php /* database configuaration */ define('DB_SERVER', 'localhost'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', ''); define('DB_DATABASE', 'phptutorial'); $con = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE); // Check connectionif (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_errno(); exit();}?>

After the creation of a configuration file for database connection

The next step is to insert the image into the database.

To achieve the inserting of image into database we have to upload image.

To upload image, create form consisting with input field.

Below is the php script from which you come to know how the image file is uploaded.

<?php include("config.php");?><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>How to fetch image from database in php</title></head><body><div style="background-color:rgba(100,200,255,0.5);" align="center"> <form action="uploading.php" method="post" enctype="multipart/form-data"> <h2>Image Uploading Section | <a href="show.php" >Veiw All Image</a></h2><br><br> <table> <tr><td height="70" width="100">Title</td><td><input type="text" autocomplete="off" required="required" name="ntitle" class="resizedTextbox"><br><br></td></tr> <tr><td height="70" width="100">Type</td><td><select name="ntype" id="ntype"><option vlalue="Banner">Banner</option><option vlalue="Logo">Logo</option></select><br><br></td></tr> <tr><td colspan="2"><input type="file" class="cck" name="sfile" id="fileName" accept="image/*"><br><br></td></tr> <tr><td colspan="2"><input class="cck" type="submit" name="notification" value="Upload"/></td></tr> </table></form></div></body></html>

Output of above code is as follow as:

Image Upload
Image Upload

As you can see the declaration of form with action calling file uploading.php.

In that file there php script for uploading of image into the database.

Below is the script of uploading image file which is called on click of upload button in the form.

<?php // Include config file include("config.php");if(isset($_POST['notification'])){$title = $_POST['ntitle'];$type = $_POST['ntype'];$uploadtime = date("Y-m-d H:i:s");$cname = $_FILES['sfile']['name'];}if(!empty($cname)){$tname = $_FILES['sfile']['tmp_name'];$tname = $_FILES['sfile']['tmp_name'];$size= $_FILES['sfile']['size'];$name = $_SESSION['firstname'].date("his");$fext = pathinfo($cname, PATHINFO_EXTENSION);$fire = pathinfo($name,PATHINFO_FILENAME);$savename = $fire.".".$fext;$finalfile = "image/$savename";}if(!empty($cname)){ if($size < 500000){$check = move_uploaded_file($tname,$finalfile);if($check){ $qry1 = "INSERT INTO `image`(`title`, `imagename`, `type`,`time`) VALUES ('$title','$savename','$type','$uploadtime')"; $test =mysqli_query($con,$qry1); if($test = TRUE){?><script>if(confirm("File Uploaded Sucessfully!!!")){document.location = 'show.php';}else{document.location = 'show.php';}</script><?php}else {?><script>if(confirm("file is not uploaded")){document.location = 'show.php';}else{document.location = 'show.php';}</script><?php}} } else{ echo "file size is too large"; } }else {echo "Please select an file to upload"; } ?>

After clicking on upload button, image get inserted into database and image file is uploaded in folder path of image as it is give in above php script.

To understand more thoroughly below is folder structure.

Folder Structure Image
Folder Structure Image

After uploading two images, the two image get inserted into database.

Image Inserted into database
Image Inserted into database

And Image file is uploaded in image folder.

If you want know more about phpMyAdmin, check with below link book which is a step-by-step instructional guide to get you started easily with phpMyAdmin and teach you to manage and perform database functions on your database.

Mastering phpMyAdmin 3.4 for Effective MySQL Management


Mastering PHP, MySQL and Javascript: A Step-by-Step Guide to Creating Dynamic Websites

Image Uploaded and Inserted in database
Image Uploaded and Inserted in database

Now fetching the image from database.

To fetch the image from the database table we have to execute select SQL query depending upon the number of image requirement.

As in the above code you can see that after inserting of images into database successfully it redirect to display image file for display of image.

The file for display of images is called as the file name show.php

<?php // Include config file include("config.php"); $baseurl = "http://".$_SERVER['SERVER_NAME']."/";?><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>How to fetch image from database in php</title> <link rel="stylesheet" href=""> <script src=""></script> <script src=""></script></head><body> <table class="table table-bordered" border="2"><tr> <td colspan="5"><center><h3>Previously Uploaded | <a href="index.php" >Add Image</a></td></tr> <tr> <tr><center><th>Title</th><th>Image</th><th>Type</th><th>Action</th></center></tr> <?php $fetchqry = "SELECT * FROM `image` ORDER BY time DESC"; $result=mysqli_query($con,$fetchqry); $num=mysqli_num_rows($result); if($num > 0){ while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ ?> <tr><td><p><?php echo $row['id']; ?><?php echo $row['title'];?></p></td><td><img width="auto" src="image/<?php echo $row['imagename']?>" width="450" height="200" /> </td> <td><?php echo $row['type']; ?></td><td width="10%"><input type="button" class="btn" value="Delete" id="button" onclick="deleteme(<?php echo $row['id']; ?>)"></td></tr> <?php } } ?> </tr></table></div><script language="javascript"> function deleteme(delid) { window.location.href='deleteimage.php?id='+delid+''; return true; } </script></body></html>

After uploading of images the output is given as below.

Fetched Image from Database table.
Fetched Image from Database table.

Finally done with the display of image from the database.

Now we going to discuss other related topic.

How to fetch image from database in php and display in table

Above is the full example of displaying of image in table. You can check with show file in which all image is get fetch using select query and display the result in table.

How to fetch image from database in php and display in div

To Display the Image from database in div tag just replace with table tag in show file in the above code.

After doing this data get display in the div tag of HTML.

How to display data in Div using PHP

How to fetch multiple image from database in php and display in table

Fetching of Multiple images depends upon select SQL query which is used in the above code in show file. Just execute below SQL query and fetched all images run in a while loop.


How to fetch uploaded image from database in php

To fetch upload image from database is similar as we discussed above, so check with the all point above.

How to fetch image from database in php and display

To get image form the database and display the image from the database which we have discussed in above example.

First we have to insert the image into the database with the form submit which shown in above code.

To get image from the database is given in show file in above.

So go through all file and execute the code and image get display.


Finally, we done with all discussion regarding how to fetch image from database in php.

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.