This content originally appeared on DEV Community and was authored by https://www.pakainfo.com/
How to fetch image from Database in PHP and Display in Table : It is simple to fetch image from database in PHP and display in table format with example and code.
Fetching image from the database using SELECT query and mysqli_fetch_array() function in the table format.
How To Fetch Image From Database In Php And Display In Table?
In this article, you going to see how to fetch image from database in php including other related topics of fetching of image.
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
Create MySql Database table
Upload image and store data in database table using PHP
Fetch image and data from database using PHP
More Details: How To Fetch Image From Database In Php And Display In Table?
Step 1: Connection with Database
For this we have create a table product in ebhor database. The dbConn.php file is used to connect with the database.
Make dbConn.php file as a common file, for reusability which always connected with MySQL database. Include only the dbConn.php file name on that file where you want to perform a task with the database. You don't need to make a connection every time in every file.
dbConn.php
<?php
$db = mysqli_connect("localhost","root","","testDB"); // database connection
if(!$db)
{
die("Connection failed: " . mysqli_connect_error());
}
?>
Step 2: Fetching image from Database Code
Here, we are fetching an image from the database into the table format.
The index.php file is using for displaying images.
In this file, we are using a table for displaying records and images in the proper format.
We are fetching records using mysqli_fetch_array() function from the database. You can see the above example there are fetched data with the images.
In this example, you can see the data in the table format for proper view. And we have also set the width and height of those images. You can also define image size as you want.
After completing all, close the database connection.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Fetch image from database in PHP</title>
</head>
<body>
<h2>All Records</h2>
<table border="2">
<tr>
<td>Sr.No.</td>
<td>Name</td>
<td>Images</td>
</tr>
<?php
include "dbConn.php"; // Using database connection file here
$records = mysqli_query($db,"select * from tbltest"); // fetch data from database
while($data = mysqli_fetch_array($records))
{
?>
<tr>
<td><?php echo $data['id']; ?></td>
<td><?php echo $data['fname']; ?></td>
<td><img src="<?php echo $data['images']; ?>" width="100" height="100"></td>
</tr>
<?php
}
?>
</table>
<?php mysqli_close($db); // close connection ?>
</body>
</html>
More Details: How To Fetch Image From Database In Php And Display In Table?
Conclusion:
Finally, we done with all discussion regarding how to fetch image from database in php.
I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.
This content originally appeared on DEV Community and was authored by https://www.pakainfo.com/
https://www.pakainfo.com/ | Sciencx (2021-08-10T13:29:31+00:00) How To Fetch Image From Database In Php And Display In Table?. Retrieved from https://www.scien.cx/2021/08/10/how-to-fetch-image-from-database-in-php-and-display-in-table/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.