how to display an image in php from mysql database example

how to display an image in php from mysql database example – i will display the image content from the MySQL database and list them on the web page. In mysql db table i have store image in column with data type blob.

how to display an image in php from mysql database example?

It is simple to fetch image/profile DP from MySQL database in PHP and display/show in table format with example as well as source code.

Insert Image File in MySQL

store_retrieve_image_from_database/
├── database_connection.php
├── index.php
├── do_upload.php
├── list.php
└── css/
    └── style.css

Store and Retrieve Image from MySQL Database using PHP

Create Database Table

CREATE TABLE `profiles` (
  `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;

Database Configuration

database_connection.php
first of all Database configuration and Create database connection afetr that Check connection.

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

Image Upload Form

don’t miss : How To Upload Image In Php And Store In Database And Folder?

Store Image File in Database

do_upload.php
first of all Include the database configuration file and then If file upload form is submitted and Get file info and check or Allow certain file formats. after that Insert image content into database with Display status message.

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

Retrieve image from database

list.php
first of all Include the database configuration file and Get image data from database.

query("SELECT image FROM profiles ORDER BY id DESC"); 
?>

num_rows > 0){ ?> 
     
 
    

Image(s) not found...

I hope you get an idea about how to display an image in php from mysql database example?.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment