How fetch image from database in php and display in form?

how to fetch image from database in php and display in table : Firstly, create a connection to the MySQL database. and fetch image from folder in PHP and display in table.

  • how to fetch image from database in php and display in table?
    • Step 1 Connection with Database
    • Step 2 Fetching image from Database Code
    • Related posts

Fetching image from MySQL database in PHP as well as display in HTML table is Like to fetch any data from database and show in HTML Table.

To fetch image from url we use HTML tag as below

" width="100" height="100">
  	








Don’t Miss How to fetch image from database in PHP and display?

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.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

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.

Generally, when we upload image file in PHP, the uploaded image is stored in a directory of the server and the respective image name is stored in the database. At the time of display, the file is retrieved from the server and the image is rendered on the web page. But, if you don’t want to consume the space of the server, the file can be stored in the database only. You can upload an image without storing the file physically on the server using the MySQL database. It’s very easy to store and retrieve images from the database using PHP and MySQL.

If you’re concerned about the server space and need free space on your server, you can insert the image file directly in the database without uploading it to the directory of the server. This procedure helps to optimize the server space because the image file content is stored in the database rather than the server. In this tutorial, we will show you how to store image files into the MySQL database and retrieve images from the database using PHP.

Before getting started to integrate file upload with the database, take a look at the file structure.

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

Insert Image File in MySQL

MySQL has a BLOB [binary large object] data type that can hold a large amount of binary data. The BLOB data type is perfect for storing image data in the database. In MySQL, four BLOB types are available – TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. The LONGBLOB data type is perfect to store the image file data.

Create Database Table

To store the file content, a table is required in the database. The following SQL creates an images table with the LONGBLOB data type field in the MySQL database.

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;

Database Configuration [dbConfig.php]

The dbConfig.php file is used to connect and select the database. Specify the database host [$dbHost], username [$dbUsername], password [$dbPassword], and name [$dbName] as per your MySQL database credentials.

Retrieve image from database [view.php]

In the view.php file, we will retrieve the image content from the MySQL database and list them on the web page.

  • The data, charset, and base64 parameters in the src attribute, are used to display image BLOB from MySQL database.


 
    
        
            

Chủ Đề