How to fetch data from localserver database and display on HTML table using PHP ?
Last Updated :
06 Jun, 2022
In this article, we will see how we can display the records in an HTML table by fetching them from the MySQL database using PHP.
Approach: Make sure you have XAMPP or WAMP server installed on your machine. In this article, we will be using the WAMP server.
WAMP Server is open-source software for the Microsoft Windows operating system, developed by Romain Bourdon. It is composed of an Apache web server, OpenSSL for SSL support, MySQL database and PHP programming language. Here, before going through the program, we need to create a MySQL database in our localhost server. Then, we are supposed to make an HTML table that is linked with PHP codes. PHP is used to connect with the localhost server and to fetch the data from the database table present in our localhost server by evaluating the MySQL queries. WAMP server helps to start Apache and MySQL and connect them with the PHP file.
Follow the steps given below:
1. Creating Database: First, we will create a database named 'geeksforgeeks'. You can use your existing database or create a new one.
create database "geeksforgeeks"
2. Create Table: Create a table named 'userdata'. The table contains four fields:
- username - varchar(100)
- problems - int(11)
- score - int(11)
- articles - int(11)
Your table structure should look like this:
the table structure of "userdata"
Or you can create a table by copying and pasting the following code into the SQL panel of your PHPMyAdmin.
CREATE TABLE IF NOT EXISTS `userdata` (
`username` varchar(100) NOT NULL,
`problems` int(11) NOT NULL,
`score` int(11) NOT NULL,
`articles` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
To do this from the SQL panel refer to the following screenshot:
create a table 'userdata" from the SQL panel
Insert records: We will now insert some records into our table. Here we are inserting 5 records. You can add multiple records.
Or copy and paste the following code into the SQL panel to insert records in the table.
INSERT INTO `userdata`
(`username`, `problems`, `score`, `articles`)
VALUES ('User-2', '100', '75', '30'), ('User-1', '150', '100', '30'), ('User-3', '200', '50', '10'), ('User-4', '50', '5', '2'), ('User-5', '0', '0', '1');
To do this from the SQL panel refer to the following screenshot:
inserting records
Creating folder and files:
We will now create our project folder named "GeeksForGeeks". Create an index.php file. Keep your main project folder (for example here.. GeeksForGeeks) in the "C://wamp64/www/", if you are using WAMP or "C://xampp/htdocs/" folder if you are using the XAMPP server respectively. The folder structure should look like this:
folder structure
Now, we have a database named geeksforgeeks, and a table named userdata. Now, here is the PHP code to fetch data from the database and display it in an HTML table.
Example:
php
<!-- PHP code to establish connection with the localserver -->
<?php
// Username is root
$user = 'root';
$password = '';
// Database name is geeksforgeeks
$database = 'geeksforgeeks';
// Server is localhost with
// port number 3306
$servername='localhost:3306';
$mysqli = new mysqli($servername, $user,
$password, $database);
// Checking for connections
if ($mysqli->connect_error) {
die('Connect Error (' .
$mysqli->connect_errno . ') '.
$mysqli->connect_error);
}
// SQL query to select data from database
$sql = " SELECT * FROM userdata ORDER BY score DESC ";
$result = $mysqli->query($sql);
$mysqli->close();
?>
<!-- HTML code to display data in tabular format -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GFG User Details</title>
<!-- CSS FOR STYLING THE PAGE -->
<style>
table {
margin: 0 auto;
font-size: large;
border: 1px solid black;
}
h1 {
text-align: center;
color: #006600;
font-size: xx-large;
font-family: 'Gill Sans', 'Gill Sans MT',
' Calibri', 'Trebuchet MS', 'sans-serif';
}
td {
background-color: #E4F5D4;
border: 1px solid black;
}
th,
td {
font-weight: bold;
border: 1px solid black;
padding: 10px;
text-align: center;
}
td {
font-weight: lighter;
}
</style>
</head>
<body>
<section>
<h1>GeeksForGeeks</h1>
<!-- TABLE CONSTRUCTION -->
<table>
<tr>
<th>GFG UserHandle</th>
<th>Practice Problems</th>
<th>Coding Score</th>
<th>GFG Articles</th>
</tr>
<!-- PHP CODE TO FETCH DATA FROM ROWS -->
<?php
// LOOP TILL END OF DATA
while($rows=$result->fetch_assoc())
{
?>
<tr>
<!-- FETCHING DATA FROM EACH
ROW OF EVERY COLUMN -->
<td><?php echo $rows['username'];?></td>
<td><?php echo $rows['problems'];?></td>
<td><?php echo $rows['score'];?></td>
<td><?php echo $rows['articles'];?></td>
</tr>
<?php
}
?>
</table>
</section>
</body>
</html>
Output: Finally, you should be able to display the records in an HTML table by fetching them from the database.
output
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read