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

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

We generally store image at server and its path on database table.

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

Steps to fetch images and data from database is as below

  1. Create MySql Database table
  2. Upload image and store data in database table using PHP
  3. Fetch image and data from database using PHP

1 Create MySql Database Table

For this we have create 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 image is uploded.

You can learn more about PHP and MySql.

3 Fetch image and data from database using PHP

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

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

we will consider here procedural way to access data.

This is a simple PHP MySql select statment.

Steps to access mysql database with PHP

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

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

To fetch image from url we use 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

Resources:

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