How to fetch image from database in php ?

In this big and best tutorials, you going to learn how to fetch image from MySQL database in php including other related steps of fetching of image.

Fetch-Data-from-Database-in-PHP
Fetch-Data-from-Database-in-PHP

Related steps are as follow as:

  • The first step is to fetch image and display in table
  • The second step is to fetch image and display in div
  • The third step is to fetch multiple image and display in table
  • Forth step is to fetch uploaded image
  • Fifth step id how to fetch image and display

Above step you learn in more information.

Lets start with the step of talked about regarding : How to fetch image from MySQL database in php

Before start with the talked about regarding fetching of image.

First step to create table name called image in the MySQL database.

Below is the SQL query execute or run in phpmyadmin so table is created.

--
-- Table structure for table `image`
--
CREATE TABLE `image` (
  `id` int(11) NOT NULL,
  `title` varchar(500) NOT NULL,
  `imagename` text NOT NULL,
  `type` varchar(500) NOT NULL,
  `time` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

After executing the above Sql query table is created as shown below.

Also Read: how to fetch images from database in php?

Now create the configuration file to configure MySQL database connection file name database_connect.php

database_connect.php


After the creation of a configuration file for MySQL database connection

Created Table Image
Created Table Image

The next step is to insert the image into the MySQL database.

To achieve the inserting of image into MySQL database I have to upload image.

To upload image, create form consisting with input field.

Below is the php script from which you come to know how the image file is uploaded.





    
    How to fetch image from MySQL database in php


Image Uploading Section | Veiw All Image



Title

Type



Output of above source code is as follow as:

Image Upload
Image Upload

As you can see the declaration of form with action calling file uploading.php.

In that file there php script for uploading of image into the MySQL database.

Also Read: Ajax Image Upload without Refreshing Page using Jquery

Below is the script of uploading image file which is called on click of upload button in the form.


				
				
					
				

After clicking on upload button, image get inserted into MySQL database and image file is uploaded in folder path of image as it is give in above php script.

To understand more thoroughly below is folder structure.

Folder Structure Image
Folder Structure Image

After uploading two images, the two image get inserted into MySQL database.

Image Inserted into database
Image Inserted into database

And Image file is uploaded in image folder.
Image Uploaded and Inserted in database
Image Uploaded and Inserted in database

Now fetching the image from MySQL database.

To fetch the image from the MySQL database table I have to execute select SQL query depending upon the number of image requirement.

As in the above code you can see that after inserting of images into MySQL database successfully it redirect to display image file for display of image.

The file for display of images is called as the file name users_list.php

users_list.php





    
    How to fetch image from MySQL database in php
    
    
    


    
0){ while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){ ?>

Previously Uploaded | Add Image

TitleImageTypeAction

After uploading of images the output is given as below.

Fetched Image from Database table.
Fetched Image from Database table.

Finally done with the display of image from the MySQL database.

Now I am going to talked about other related step.

How to fetch image from MySQL database in php and display in table

Above is the full example of displaying of image in table. You can check with users_list.php file in which all image is get fetch using select query as well as display the result in table.

How to fetch image from MySQL database in php and display in div

To Display the Image from MySQL database in div tag just replace with table tag in users_list.php file in the above source code.

After doing this data get display in the div tag of HTML.

How to fetch multiple image from MySQL database in php and display in table

Fetching of Multiple images depends upon select SQL query which is used in the above complete code in users_list.php file. Just execute below SQL query and fetched all images run in a while loop.

SELECT * FROM `image` ORDER BY time DESC

Also Read: insert delete image from folder in php with jQuery

How to fetch uploaded image from MySQL database in php

To fetch upload image from MySQL database is similar as I talked about above mysql Query, so check with the all point above.

How to fetch image from MySQL database in php and display

To get image HTML form the MySQL database and display the image from the MySQL database which I have talked about in above example.

First I have to insert the image into the MySQL database with the HTML form submit which shown in above full source code.

To get image from the MySQL database is given in users_list file in above.

So go through all file and execute the code and image get display.

Conclusion:

Finally, I done with all talked about regarding how to fetch image from MySQL database in php.

I hope you liked my this Tutorials. If you have any queries or any question regarding this, Feel free to comment on Me.

Leave a Comment