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?

index.php




get PNG, JPG or JPEG Image retrieve



"; echo ""; while($product=mysqli_fetch_array($res)) { echo ""; echo 'how to fetch images from database in php'; echo "
"; ?>">Delete "; } echo ""; echo ""; } ?>

Also Read: Ajax Image Upload without Refreshing Page using Jquery

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

Create Database Table

CREATE TABLE `products`

CREATE TABLE `products` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `image` longblob NOT NULL,
 `uploaded` datetime NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Database Configuration

database_connection.php

connect_error) {  
    die("Connection failed: " . $db->connect_error);  
}

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

Image Upload Form

html code

Store Image File in Database

upload.php

query("INSERT into products (image, uploaded) VALUES ('$imgContent', NOW())"); 
             
            if($insert){ 
                $status = 'success'; 
                $saveImagesMessage = "Profile uploaded successfully."; 
            }else{ 
                $saveImagesMessage = "Profile upload failed, please try again."; 
            }  
        }else{ 
            $saveImagesMessage = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.'; 
        } 
    }else{ 
        $saveImagesMessage = 'Please select an image file to upload.'; 
    } 
} 
 
// Display status message 
echo $saveImagesMessage; 
?>

Also Read: Capture Webcam Image with PHP and jQuery

Retrieve image from database

view.php

query("SELECT image FROM products ORDER BY uploaded DESC"); 
?>

num_rows > 0){ ?> 
     
 
    

Your Image(s) not found...

Leave a Comment