How to fetch image from database in PHP and display in table

Fetching an image from a database in PHP and display in the table is similar to fetch any data from a database and show in HTML Table.

We generally store images on the server and their path on the database table.

We are considering an example of products to demonstrate the purpose.

The steps to fetch images and data from the database is as below

  1. Create a MySql database table
  2. Upload images and store data in a database table using PHP
  3. Fetch images and data from the database using PHP

1 Create MySql Database Table

For this, we have created a table product in ebhor database.

2 Upload image and store data in database table using PHP

Uploading images and data are not discussed here.

After uploading images and data our product table will look like this.

PHPMyAdmin Product Table
PHPMyAdmin Product Table

We are providing above data for copy and create your database

Here Image column contains the URL where the image is uploaded.

You can learn more about PHP and MySql.

3 Fetch image and data from database using PHP

As you have already seen images are stored in the server file system and its path is stored in the database.

To access data from the database we can use either the Procedural way using mysqli or PDO or the object-oriented way.

we will consider here a procedural way to access data.

This is a simple PHP MySql select statement.

Steps to access MySQL database with PHP

  1. Connecting PHP with MySql using mysqli_connect(“localhost”, “root”, “”, “ebhor”); localhost means the database is in the same system, the username is the root, the password is “”, and ebhor is the database name.
  2. check whether the connection is established or not if $connect==false means the connection is not established.
  3. Create a query and pass it to mysqli_query(…..)
  4. It will return results in $result.
  5. if there are results then iterate each row and print in tabular format

Display products from database in PHP

By iterating mysqli_fetch_array($result) we are fetching rows one by one and showing in tabular format.

To fetch images from URL we use an HTML tag <img src =""> as below

Read More

  1. PHP MySQL CRUD Tutorial with MySqli and PHPMyAdmin
  2. How to Export and Import MySql Database using phpMyAdmin
  3. PDO in PHP


Photo by Pok Rie from Pexels
Image by ADMC from Pixabay
Photo by C D-X on Unsplash
Photo by ATC Comm Photo from Pexels