how to fetch image from database in php

how to fetch image from database in php? OR Store and Retrieve Image from MySQL Database using PHP.

how to fetch image from database in php?

Firstly, create a connection to the MySQL database. · After that, Write input fields to store images using Php.

how to fetch image from database in php
how to fetch image from database in php

store_retrieve_image_from_database/
├── dbConfig.php
├── index.php
├── upload.php
├── view.php
└── css/
└── style.css

Don’t Miss : How to fetch image from database in php and display in table?

Create Database Table

[php]
CREATE TABLE `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` longblob NOT NULL,
`created` datetime NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
[/php]

Database Configuration (dbConfig.php)

[php]
connect_error) {
die(“Connection failed: ” . $db->connect_error);
}
[/php]

Image Upload Form

[php]




[/php]

Store Image File in Database (upload.php)

[php]
query(“INSERT into images (image, created) VALUES (‘$imgContent’, NOW())”);

if($insert){
$status = ‘success’;
$userMessage = “File uploaded successfully.”;
}else{
$userMessage = “File upload failed, please try again.”;
}
}else{
$userMessage = ‘Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.’;
}
}else{
$userMessage = ‘Please select an image file to upload.’;
}
}

echo $userMessage;
?>
[/php]

Retrieve image from database (view.php)

[php]
query(“SELECT image FROM images ORDER BY id DESC”);
?>

num_rows > 0){ ?>

Leave a Comment