how to display an image in php from mysql database example

0
()

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.

<?php  
$dbHost     = "localhost";  
$dbUsername = "root";  
$dbPassword = "DFhj#54872";  
$dbName     = "pakainfo_v1";  
$link = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);  
   
if ($link->connect_error) {  
    die("Connection failed: " . $link->connect_error);  
}

Image Upload Form

<form action="do_upload.php" method="post" enctype="multipart/form-data">
    <label>Select Profile File:</label>
    <input type="file" name="image">
    <input type="submit" name="submit" value="Upload">
</form>

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

Read Also:  MySQL SELECT DISTINCT, COUNT, ROWS EXAMPLE

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.

<?php 
  
require_once 'database_connection.php'; 
$status = $dispMessage = ''; 
if(isset($_POST["submit"])){ 
    $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 = $link->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; 
?>

Free Live Chat for Any Issue

Retrieve image from database

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

<?php 
require_once 'database_connection.php'; 
$output = $link->query("SELECT image FROM profiles ORDER BY id DESC"); 
?>

<?php if($output->num_rows > 0){ ?> 
    <div class="gallery"> 
        <?php while($row = $output->fetch_assoc()){ ?> 
            <img src="data:image/jpg;charset=utf8;base64,<?php echo base64_encode($row['image']); ?>" /> 
        <?php } ?> 
    </div> 
<?php }else{ ?> 
    <p class="status error">Image(s) not found...</p> 
<?php } ?>

Download

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.

Read Also:  PayPal Payment Gateway with PHP MySQL Database

How useful was this post?

Click on a star to rate it!