how to display data from database in php in table?

how to display data from database in php in table? – Basic Program using Loop, Using jQuery Ajax and Using Data Table. retrieve data from db and display it in table in php Example

How to fetch data from Database in PHP and display in HTML table?

how to display data from database in php in table? simple step by step learn to Fetch Data From Database in PHP and Display in HTML Table Example with demo.

1) Basic Program using Loop

<?php
$host = "127.0.0.1"; 
$userName = "root"; 
$userPass = ""; 
$database = "pakainfo_v1";

$link = mysqli_connect($host,$userName,$userPass,$database);

if(mysqli_connect_errno()){
    echo mysqli_connect_error();
    exit();
}else{
    $sqlQQ = "SELECT * FROM `tbl_members` ORDER BY `member_id` ASC";
    $result = mysqli_query($link,$sqlQQ);
    if(mysqli_num_rows($result) > 0){
    }else{
        $msg = "No Member Record found";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HTML and PHP code</title>
</head>
<body>
    <h1>Display member list using HTML and PHP</h1>
    <?=$msg;?>
    <table border="1px" style="width:600px; line-height:40px;">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Status</th>
                <th>Registrating Date</th>
            </tr>
        </thead>
        <tbody>
            <?php
                while($row = mysqli_fetch_assoc($result)){?>
                <tr>
                    <td><?php echo $row['member_firstName'].$row['member_lastName']; ?></td>
                    <td><?php echo $row['member_email']; ?></td>
                    <td><?php if($row['member_status'] == 1){
                        echo "Active";
                    }else{
                        echo "Deactive";
                    } ?></td>
                    <td><?php echo $row['member_registrationDate']; ?></td>
                <tr>
            <?}?>
        </tbody>
    </table>
</body>
</html>

step : 2) Using Ajax

HTML File Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HTML and PHP source code</title>
</head>
<body>
    <h3>Display member list using HTML and PHP</h3>
    <?=$msg;?>
    <table border="1px" style="width:600px; line-height:40px;">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Registrating Date</th>
            </tr>
        </thead>
        <tbody id="tableBody">
            
        </tbody>
    </table>
</body>
</html>

<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

<script>
$( document ).ready(function() {
    $.ajax({
        url: 'fetch.php',
        mothod: 'post',
        dataType: 'json',
        success:function(data){
            let string = '';
            $.each(data, function(key, value){
                string += `<tr>
                <td>${value['member_firstName']} ${value['member_lastName']}</td>
                <td>${value['member_email']}</td>
                <td>${value['member_registrationDate']}</td>
                </tr>`;
            });
            $('#tableBody').append(string);
        },
        error:{

        }
    });
});
</script>

PHP File Code

<?php
//how to display data from database in php in table
$host = "127.0.0.1"; 
$userName = "root"; 
$userPass = ""; 
$database = "pakainfo_v1"; 

$link = mysqli_connect($host,$userName,$userPass,$database);

if(mysqli_connect_errno()){
    echo mysqli_connect_error();
    exit();
}else{
    $sqlQQ = "SELECT * FROM `tbl_members` ORDER BY `member_id` ASC";
    $result = mysqli_query($link,$sqlQQ);
    if(mysqli_num_rows($result) > 0){
        $result_array = array();
        while($row = mysqli_fetch_assoc($result)){
            array_push($result_array, $row);
        }

    }

    echo json_encode($result_array);

}
?>

Step : 3) Using Data Table

HTML File Code

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HTML and PHP code</title>
    <style>
        table, td, th {
            border: 1px solid #ddd;
            text-align: left;
        }

        table {
            border-collapse: collapse;
            width: 100%;
        }

        th, td {
            padding: 15px;
        }
    </style>
</head>

<body>
    <h3>Display member list using HTML and PHP</h3>
    <?= $msg; ?>
    <table id="my-example">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Registrating Date</th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
</body>

</html>

<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js"></script>

<script>
    $(document).ready(function() {
        $('#my-example').dataTable({
            "bProcessing": true,
            "sAjaxSource": "fetch.php",
            "aoColumns": [{
                    mData: 'member_firstName'
                },
                {
                    mData: 'member_email'
                },
                {
                    mData: 'member_registrationDate'
                }
            ]
        });
    });
</script>

Don’t Miss : how to search and display data from database in php?

Read Also:  Vue Laravel CRUD Routing Single Page Application

PHP File Source Code

<?php
$host = "127.0.0.1"; 
$userName = "root"; 
$userPass = ""; 
$database = "pakainfo_v1";

$link = mysqli_connect($host,$userName,$userPass,$database);

if(mysqli_connect_errno()){
    echo mysqli_connect_error();
    exit();
}else{
    $sqlQQ = "SELECT * FROM `tbl_members` ORDER BY `member_id` ASC";
    $result = mysqli_query($link,$sqlQQ);
    if(mysqli_num_rows($result) > 0){
        $result_array = array();
        while($row = mysqli_fetch_assoc($result)){
            array_push($result_array, $row);
        }

    }

    $results = ["sEcho" => 1,
        	"iTotalRecords" => count($result_array),
        	"iTotalDisplayRecords" => count($result_array),
        	"aaData" => $result_array ];

    echo json_encode($results);

}
?>

I hope you get an idea about how to display data from database in php in table.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.