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:  Access Tokens - Facebook Login - Facebook Open Graph API

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:  how to upload image in php and store in database and folder?

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:  Laravel 5.7 Toggle switch Update DB field using Ajax

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 } ?>

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  How to Store and Retrieve Image from database in Php?
  2. Read Also:  Laravel Selecting and Aliasing Columns in Eloquent's query
  3. Read Also:  how to show an image in php from mysql database example?
  4. Read Also:  insert delete image from folder in php with jQuery
  5. Read Also:  Simple vue.js Conditional Directives
  6. Read Also:  Convert and Loop through JSON with PHP and JavaScript Arrays/Objects | json to array php
  7. Read Also:  How to get current Date and Time using JavaScript
  8. Read Also:  How to use multiple databases in Laravel
  9. Read Also:  Laravel Drop all tables with MySQL
  10. Read Also:  PHP MySQL CRUD Create, Insert, Update and Delete operations