How to fetch image from database in php and display in table? – Store and Retrieve Image from MySQL Database using PHP Full Source code. Connect PHP with MySql connection Create a query return result show HTML Table.
Table of Contents
how to fetch image from database in php and display in table?
my_shop/
├── connection.php
├── index.php
├── do_upload.php
├── view.php
└── css/
└── style.css
Step 1: Create Database Table
[php]
CREATE TABLE `members` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` longblob NOT NULL,
`created_at` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
[/php]
Step 2: Database Configuration
connection.php
[php]
connect_error) {
die(“Connection failed: ” . $db->connect_error);
}
[/php]
Step 3: Image Upload Form
[php]
[/php]
Step 4: Store Image File in Database
do_upload.php
[php]
query(“INSERT into members (image, created_at) VALUES (‘$imgContent’, NOW())”);
if($insert){
$is_available = ‘success’;
$displayNotify = “File uploaded successfully.”;
}else{
$displayNotify = “File upload failed, please try again.”;
}
}else{
$displayNotify = ‘Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.’;
}
}else{
$displayNotify = ‘Please select an image file to upload.’;
}
}
echo $displayNotify;
?>
[/php]
Step 5: Retrieve image from database
view.php
[php]
query(“SELECT image FROM members ORDER BY id DESC”);
?>
num_rows > 0){ ?>
Image(s) not found…
[/php]