How to Fetch and Display Data From Database in PHP in Table
Retrieve or fetch the data from the MySQL database in PHP and display table; In this tutorial, we will show you how to fetch/select/retrieve the data from the database in PHP and display it in the bootstrap Table.
First of all, we will create one database connecting file, And create html table web page with display data from database in PHP.
PHP Code Retrieve Data from the Database and Display in HTML Table
To retrieve data from MySQL, the SELECT statement is used. That can retrieve data from a specific column or all columns of a table.
If you want to get selected column data from the database. Use the below SQL query is:
SELECT column_name,column_name FROM table_name;
If you want to get all the columns data from a table. Use the below SQL query is:
PHP code to fetch data from MySQL database and display in HTML table
1. Connecting to the database in PHP
In this step, you will create a file name db.php and update the below code into your file.
The below code is used to create a MySQL database connection in PHP. When we fetch, insert, update or delete data from MySQL database, there we will include this file:
2. Fetch data from the database and display in table
In this step, we will fetch the data from the MySQL database in PHP and display data in an HTML table. So you can create a new file and update the below code into your file.
The below code is used to retrieve or receive data from the MySQL database in PHP. Additionally, we will display the fetched data in an HTML table.
.bs-example
0) < ?>
Name
Email id
Mobile
?>
else < echo "No result found"; >?>
Conclusion
To retrieve or fetch or get the data from the MySQL database in PHP. Here you have learned how to fetch and display data in an HTML table using PHP MySQL.
This is a very basic and easy example of fetching the data into a MySQL database using PHP script. In the next tutorial, we will show you how you can update from data into the MySQL database in PHP.
Hello Developer, In this tutorial, You will learn to display data in an HTML table using PHP and MySQL. Even You will know more to fetch new records from the database and show them in the tabular format using MySQLi procedure, MySQLi Object-oriented, PDO & prepared statement with a new concept & an example.
Steps to Display Data From MySQL Database with PHP
In this step, you will learn to display data from the database dynamically with an advanced coding concept that will help you to improve your coding skills for writing smart & standard code in PHP.
Before getting started, make sure that the local server (xamp/wamp) must be installed in your system and start it if it is not being started already.
Learn Also –
You can test yourself to display data with the following folder structure –
Before displaying data, you have to insert data into the Database. If you have not already done it and don’t know about it, then you can learn it through the following URL –
3. Fetch Data From MySQL Table
Now, You have to fetch data from the MySQL table. So, just follow these points –
First of all, include a database connection file database.php
assign $conn to a new variable $db and table name to another variable $table
Define columns name in an indexed array and assign them to the $columns
Also, assign fetch_data() function to the $fetchData
fetch_data() – This function accepts three parameters like $db, $table & $column and It contains MySQLi SELECT query that will return records in an array format by fetching from the database
elseif (empty($columns) || !is_array($columns)) < $msg="columns Name must be defined in an indexed array"; >elseif(empty($tableName))< $msg= "Table Name is empty"; >else< $columnName = implode(", ", $columns); $query = "SELECT ".$columnName." FROM $tableName"." ORDER BY id DESC"; $result = $db->query($query); if($result== true)< if ($result->num_rows > 0) < $row= mysqli_fetch_all($result, MYSQLI_ASSOC); $msg= $row; >else < $msg= "No Data Found"; >>else < $msg= mysqli_error($db); >> return $msg; > ?>
4. Display Data in HTML Table
Now, You have to display data in the HTML table. So, you have to follow these points –
First of all, Include PHP script file developers.php
Create an HTML table using Bootsrap 4
Check $fetchData is an array or not with if & else condition
Then apply foreach loop to the $fetchData
After that print the required data in the table
S.N
Full Name
Gender
Email
Mobile Number
Address
City
State
>else < ?> ?>
5. Test Yourself to insert data
After Implementing previous steps, You can test it yourself to open the following URL in your web browser
https://localhost/codingstatus/table.php
More Methods to display Data in HTML Table with PHP
Now, You should know more methods to display data in the database from the next step. After learning those methods, you can use one of them in your project.
From the next step, I have shared only the source code without a custom function. So that you can directly paste it into your project where you need to implement it.
Display Data From Database Into HTML Table Using PHP
Today I will share how to display data from MySQL database into HTML table using PHP. I will also show that how to store values from database table into HTML array, and how to handle that array in PHP. For this purpose first I will create a database with name allphptricks and then create a table in database with name sports and dump dummy data into it.
Steps to Display Data From Database Into HTML Table Using PHP
Create a Database
Create a Database Table
Dumping Data into Table
Create a Database Connection Page
Create a Main Index Page
1. Create a Database
Execute the following query in your MySQL query.
CREATE DATABASE allphptricks;
2. Create a Database Table
Run the following query in your above database.
CREATE TABLE IF NOT EXISTS `sports` ( `sport_id` int(10) NOT NULL AUTO_INCREMENT, `sport_name` varchar(100) NOT NULL, PRIMARY KEY (`sport_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
3. Dumping Data into Table
Now I will insert sample data in our table, following query is dumping data into table.
For your ease I have also attached an sport.sql file in this tutorial download, you can import that file if you do not want to create table and dumping data.
4. Create a Database Connection Page
Create a database connection page with name db.php and copy the following code in it.
5. Create a Main Index Page
Now create a main index.php page which contain the actual script of PHP which will show data from database into HTML table and also it will store all values in HTML array and on form submission, it will catch all selected values and print them on web page, you can also store it in your database.
Now copy the below code in your index.php page right after start of tag
HTML Form & PHP Loop Script
">
'; $count = 0; > > ?>
In the input field name I used sports[] these square brackets will make it HTML array. The above code will get all sports name from database table and view them in the HTML table form, each row display three sports name. But this will also need database connection, so copy the below code before the start of tag in the page header.
DB Connection & PHP Loop Script of Array
You selected the below sports: "; foreach($_POST['sports'] as $sport_id)< $query = mysqli_query ( $con, "SELECT * FROM sports WHERE `sport_id`='$sport_id'" ); $row = mysqli_fetch_assoc($query); $status .= $row['sport_name'] . " "; > > > ?>
Above code will not only include database connection file but it will also check if form is submitted so it will check array variable, if it is array then it will start foreach loop to catch all selected values by user and store it in $status variable which will display result below HTML table.
Little CSS of HTML Table
Add following CSS in your index.php before closing
If you found this tutorial helpful, share it with your friends and developers group.
Facebook Official Page: All PHP Tricks
Twitter Official Page: All PHP Tricks
Javed Ur Rehman is a passionate blogger and web developer, he loves to share web development tutorials and blogging tips. He usually writes about HTML, CSS, JavaScript, Jquery, Ajax, PHP and MySQL.
How To Display Data From Database Table In PHP/MySQL Using PDO Query
In this tutorial, we are going to learn on How To Display Data From Database Table In PHP/MySQL Using PDO Query. You can use this source code to merge the last tutorial that I made and it's called Registration Form In PHP/MySQL Using PDO Query. This source code will help us on how to show data from the Database using PDO Query. Let's start with:
Creating our Table
Open the PHPMyAdmin.
Create a database and name it as "registration_pdo".
After creating a database name, then we are going to create our table. And name it as "user_registration".
Data in the Database.
Database Connection
Display Page
Output:
You can see in the image lists of data in the Database. This is all the steps on How To Display Data From Database Table In PHP/MySQL Using PDO Query. So, this is it, or you can download the full source code below by clicking the "Download Code" button below. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.
Tags
Comments
nice way of prasentation and
programing
Code Lover Forever
I just learned how create a table for `user_registration` in PHP #ceos #markzuckerberg #satyanadella #libra #workingfromhome CREATE TABLE `user_registration` ( `user_id` INT(11) NOT NULL, `first_name` VARCHAR(100) NOT NULL, `last_name` VARCHAR(100) NOT NULL, `user_name` VARCHAR(100) NOT NULL, `password` VARCHAR(100) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=Indoamerican
How to show and modify just logged in user's detail?
Thanks for this tutorial but. I'd like to see after login just the user's details and also I'd like just to modify them, I don't understand how to use sessions. :\