How to Store PHP Imagejpg Result in Mysql Database

Storing images in a database can be useful for various reasons, for example, when you need to retrieve and display images in your website dynamically.

In this tutorial, we’ll learn how to store a PHP image (in .jpg format) in a MySQL database.

Before we dive into the code, let’s set up the necessary environment for this task. We’ll need a web server with PHP and MySQL installed.

If you don’t already have a web server installed, you can install a LAMP (Linux, Apache, MySQL, PHP) or a XAMPP (Apache, MySQL, PHP, and Perl) stack on your machine.


Create the Database

First, we’ll create a database to store the image.

Open your favorite database management tool, such as phpMyAdmin, and create a database with a name of your choice.

We’ll use “images_db” in this tutorial.

Next, create a table to store the image in the database.

The table should have columns for the image ID, image name, and image data (in binary format).

CREATE TABLE images (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(255) NOT NULL,
  image LONGBLOB NOT NULL
);

Upload the Image

Now that we have a database to store the image, let’s write the code to upload an image.

We’ll create an HTML form that allows users to select an image and submit it to the server.

<form action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="image">
  <input type="submit" value="Upload">
</form>

Next, we’ll write the PHP code to handle the form submission.

The code will retrieve the image from the form, store it in a variable, and insert it into the database.

<?php
  $name = $_FILES['image']['name'];
  $image = file_get_contents($_FILES['image']['tmp_name']);
  
  $db = new mysqli('localhost', 'username', 'password', 'images_db');
  
  $sql = "INSERT INTO images (name, image) VALUES (?, ?)";
  $stmt = $db->prepare($sql);
  $stmt->bind_param('ss', $name, $image);
  $stmt->execute();
  
  header('Location: index.php');
?>

Retrieve the Image

Finally, we’ll write the code to retrieve the image from the database and display it on the web page.

We’ll use the image ID to retrieve the image from the database.

<?php
  $id = $_GET['id'];
  
  $db = new mysqli('localhost', 'username', 'password', 'images_db');
  
  $sql = "SELECT image FROM images WHERE id = ?";
  $stmt = $db->prepare($sql);
  $stmt->bind_param('i', $id);
  $stmt->execute();
  $stmt->bind_result($image);
  $stmt->fetch();
  
  header('Content-Type: image/jpeg');
  echo $image;
?

Display the Image

Once we have retrieved the image data from the database, we can display it on the web page by setting the content type to “image/jpeg” and echoing the image data.

<img src="show_image.php?id=<?php echo $id; ?>">

In this way, you can store and display images in your PHP website using a MySQL database.

This approach can be useful in various applications, such as e-commerce websites, image galleries, and more.


Conclusion

In this tutorial, we have learned how to store a PHP image (in .jpg format) in a MySQL database.

We’ve seen how to create a database, upload an image using an HTML form, retrieve the image from the database,and display it on a web page.

With this knowledge, you can build dynamic websites that can store and display images from a database.