Skip to content
Related Articles

Related Articles

Improve Article

How to fetch data from localserver database and display on HTML table using PHP ?

  • Last Updated : 31 Jul, 2021

WampServer is open-source software for the Microsoft Windows operating system, developed by Romain Bourdon. It is composed of 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. Wampserver helps to start Apache and MySQL and connect them with the PHP file.

Consider, we have a database named gfg, a table named userdata. Now, here is the PHP code to fetch data from database and display in an HTML table.

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

Creating Database:


// PHP code to establish connection
// with the localserver
// Username is root
$user = 'root';
$password = ''
// Database name is gfg
$database = 'gfg'
// Server is localhost with
// port number 3308
$mysqli = new mysqli($servername, $user
                $password, $database);
// Checking for connections
if ($mysqli->connect_error) {
    die('Connect Error ('
    $mysqli->connect_errno . ') '
// SQL query to select data from database
$sql = "SELECT * FROM userdata ORDER BY score DESC ";
$result = $mysqli->query($sql);
// HTML code to display data in tabular format
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>GFG User Details</title>
        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;
        td {
            font-weight: bold;
            border: 1px solid black;
            padding: 10px;
            text-align: center;
        td {
            font-weight: lighter;
        <!-- TABLE CONSTRUCTION-->
                <th>GFG UserHandle</th>
                <th>Practice Problems</th>
                <th>Coding Score</th>
                <th>GFG Articles</th>
            <!-- PHP CODE TO FETCH DATA FROM ROWS-->
            <?php   // LOOP TILL END OF DATA 
                <!--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>


CSS is the foundation of webpages, is used for webpage development by styling websites and web apps.You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :