how to fetch images from database in php

here we learn to dynamically Store and Retrieve Image from MySQL Database using PHP jquery Ajax Example with Demo.

How to Retrieve Image In PHP?


<!DOCTYPE html>
<title>get PNG, JPG or JPEG Image retrieve</title>

<form name="form1" action="" method="post" enctype="multipart/form-data">
<td><input type="submit" name="store_img" value="display"></td>
$con = mysqli_connect('localhost','root','','gandhi_shops') or die('Unable To connect');

   $res=mysqli_query($con,"select * from products");
   echo "<table>";
   echo "<tr>";
   echo "<td>"; 
   echo '<img src="data:image/jpeg;base64,'.base64_encode($product['image'] ).'" height="150" width="150"/>';
   echo "<br>";
   ?><a href="remove.php?id=<?php echo $product["id"]; ?>">Delete</a> <?php
   echo "</td>";
   echo "</tr>";
   echo "</table>";

Also Read: Ajax Image Upload without Refreshing Page using Jquery

Read Also:  How to Store and Retrieve Image from database in Php?

how insert and retrieve images to and from database using php?

Create Database Table

CREATE TABLE `products`

CREATE TABLE `products` (
 `image` longblob NOT NULL,
 `uploaded` datetime NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Database Configuration


$dbHost     = "localhost";  
$dbUsername = "root";  
$dbPassword = "DSFdsf7d5f4d#$$dkfjd98956";  
$dbName     = "gandhi_shop";  
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);  
if ($db->connect_error) {  
    die("Connection failed: " . $db->connect_error);  

Also Read: insert delete image from folder in php with jQuery

Read Also:  insert delete image from folder in php with jQuery

Image Upload Form

html code

<form action="upload.php" method="post" enctype="multipart/form-data">
    <label>Select Image File:</label>
    <input type="file" name="image">
    <input type="submit" name="submit" value="Upload">

Store Image File in Database


require_once 'database_connection.php'; 
$status = $saveImagesMessage = ''; 
    $status = 'error'; 
    if(!empty($_FILES["image"]["name"])) { 
        $fileName = basename($_FILES["image"]["name"]); 
        $fileType = pathinfo($fileName, PATHINFO_EXTENSION); 
        $allowTypes = array('jpg','png','jpeg','gif'); 
        if(in_array($fileType, $allowTypes)){ 
            $image = $_FILES['image']['tmp_name']; 
            $imgContent = addslashes(file_get_contents($image)); 
            $insert = $db->query("INSERT into products (image, uploaded) VALUES ('$imgContent', NOW())"); 
                $status = 'success'; 
                $saveImagesMessage = "Profile uploaded successfully."; 
                $saveImagesMessage = "Profile upload failed, please try again."; 
            $saveImagesMessage = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.'; 
        $saveImagesMessage = 'Please select an image file to upload.'; 
// Display status message 
echo $saveImagesMessage; 

Also Read: Capture Webcam Image with PHP and jQuery

Read Also:  how to upload image in php and store in database and folder?

Retrieve image from database


require_once 'database_connection.php'; 

$all_products = $db->query("SELECT image FROM products ORDER BY uploaded DESC"); 

<?php if($all_products->num_rows > 0){ ?> 
    <div class="gallery"> 
        <?php while($product = $all_products->fetch_assoc()){ ?> 
            <img src="data:image/jpg;charset=utf8;base64,<?php echo base64_encode($product['image']); ?>" /> 
        <?php } ?> 
<?php }else{ ?> 
    <p class="status error">Your Image(s) not found...</p> 
<?php } ?>

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

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