How to Store and Retrieve Image from database in Php?

Today, We want to share with you store and retrieve image from database in php.In this post we will show you how to upload image in php and store in database and folder?, hear for how to display all images from database in php? we will give you demo and example for implement.In this post, we will learn about how to fetch images from database in php with an example.

Store and Retrieve Image from database in Php

index.php

<!DOCTYPE html>
<html>
<head>
<title>how to fetch image from database in php using ajax - www.pakainfo.com</title>
</head>
<body>
  
  <form method="POST" action="index.php" enctype="multipart/form-data">
  	  	
  	  <input type="file" name="image">  	
  	  <button type="submit" name="upload">Upload Profile DP</button>
  	
  </form>
  
  <?php
  // simple database db_config
  $conn = mysqli_connect("localhost", "root", "", "member");
    
  if (isset($_POST['upload'])) {
	  
  	// Get name of images
  	$fetch_profile_dp = $_FILES['image']['name'];
  	
  	// image Path
  	$profile_dp_path = "images/".basename($fetch_profile_dp);

  	$sql = "INSERT INTO member_table (profile_picture_name, contact) VALUES ('$fetch_profile_dp', 'USA')";
  	
	// Run SQL query
  	mysqli_query($conn, $sql);

  	if (move_uploaded_file($_FILES['image']['tmp_name'], $profile_dp_path)) {
  		echo "Your Profile DP uploaded successfully";
  	}else{
  		echo  "Not Insert Profile DP";
  	}
  }
  
  
  // Fetch image from database
	$img = mysqli_query($conn, "SELECT * FROM member_table");
     while ($row = mysqli_fetch_array($img)) {     
		
      	echo "<img src='images/".$row['profile_picture_name']."' >";   
      
    }     
?>
</body>
</html>

How to Display an Image in Php from MySQL Database Example

display_images.php

<!DOCTYPE html>
<html>
<head>
<title>retrieve image</title>
</head>
<body>
// database db_config
  $conn = mysqli_connect("localhost", "root", "", "member");
// Fetch image from database
	$img = mysqli_query($conn, "SELECT * FROM member_table");
     while ($row = mysqli_fetch_array($img)) {     
		
      	echo "<img src='images/".$row['profile_picture_name']."' >";   
      
    } 
</body>
</html>

How to Upload Image in PHP and Store in Database and Folder?

Step 1: Create a Database db_config

  $conn = mysqli_connect("localhost", "root", "", "member");

Step 2: Create Folder and file name with Insert Image Query MySQL code in Php.

// Get the name of images
  	$fetch_profile_dp = $_FILES['image']['name'];
  	
  	// image Path
  	$profile_dp_path = "images/".basename($fetch_profile_dp);

  	$sql = "INSERT INTO member_table (member_profile_pic, contact) VALUES ('$fetch_profile_dp', 'USA')";
  	
	// Run SQL query
  	mysqli_query($conn, $sql);

  	if (move_uploaded_file($_FILES['image']['tmp_name'], $profile_dp_path)) {
  		echo "Your Profile DP uploaded successfully";
  	}else{
  		echo  "Not Insert Profile DP";
  	}
  }

Step 3: Insert image and fetch image Html structure code

<form method="POST" action="index.php" enctype="multipart/form-data">
  	  	
  	  <input type="file" name="image">  	
  	  <button type="submit" name="upload">Upload Profile DP</button>
  	
  </form>

I hope you get an idea about how to fetch image from database in php and display in table.
I would like to have feedback on my infinityknow.com blog.
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:  Import Excel File Data In Database Using PHP

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

Your email address will not be published. Required fields are marked *