Hướng dẫn how fetch data from postgresql database in php and display html table? - cách tìm nạp dữ liệu từ cơ sở dữ liệu postgresql trong php và hiển thị bảng html?

Có một vài phương pháp sử dụng mà bạn có thể sử dụng dữ liệu tìm nạp từ cơ sở dữ liệu trong PHP và hiển thị nó vào bảng HTML. Trong hướng dẫn này, chúng tôi đã giải thích phương pháp sau đây để tìm nạp dữ liệu từ cơ sở dữ liệu trong PHP và hiển thị nó vào bảng HTML.

  • Chương trình cơ bản sử dụng vòng lặp
  • Sử dụng Ajax
  • Sử dụng bảng dữ liệu

1) Chương trình cơ bản sử dụng vòng lặp

<?php
$host = "127.0.0.1"; //IP of your database
$userName = "root"; //Username for database login
$userPass = ""; //Password associated with the username
$database = "example-database"; //Your database name

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

if(mysqli_connect_errno()){
    echo mysqli_connect_error();
    exit();
}else{
    $selectQuery = "SELECT * FROM `tbl_users` ORDER BY `user_id` ASC";
    $result = mysqli_query($connectQuery,$selectQuery);
    if(mysqli_num_rows($result) > 0){
    }else{
        $msg = "No 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>
    <h2>Display user list using HTML and PHP</h2>
    <?=$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['user_firstName'].$row['user_lastName']; ?></td>
                    <td><?php echo $row['user_email']; ?></td>
                    <td><?php if($row['user_status'] == 1){
                        echo "Active";
                    }else{
                        echo "Deactive";
                    } ?></td>
                    <td><?php echo $row['user_registrationDate']; ?></td>
                <tr>
            <?}?>
        </tbody>
    </table>
</body>
</html>

Đầu ra

Hướng dẫn how fetch data from postgresql database in php and display html table? - cách tìm nạp dữ liệu từ cơ sở dữ liệu postgresql trong php và hiển thị bảng html?

2) Sử dụng Ajax

Mã tệp HTML

<!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>
    <h2>Display user list using HTML and PHP</h2>
    <?=$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['user_firstName']} ${value['user_lastName']}</td>
                <td>${value['user_email']}</td>
                <td>${value['user_registrationDate']}</td>
                </tr>`;
            });
            $('#tableBody').append(string);
        },
        error:{

        }
    });
});
</script>

Mã tập tin PHP

<?php
$host = "127.0.0.1"; //IP of your database
$userName = "root"; //Username for database login
$userPass = ""; //Password associated with the username
$database = "example-database"; //Your database name

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

if(mysqli_connect_errno()){
    echo mysqli_connect_error();
    exit();
}else{
    $selectQuery = "SELECT * FROM `tbl_users` ORDER BY `user_id` ASC";
    $result = mysqli_query($connectQuery,$selectQuery);
    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);

}
?>

Đầu ra

Hướng dẫn how fetch data from postgresql database in php and display html table? - cách tìm nạp dữ liệu từ cơ sở dữ liệu postgresql trong php và hiển thị bảng html?

2) Sử dụng Ajax

Mã tệp HTML

<!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>
    <h2>Display user list using HTML and PHP</h2>
    <?= $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: 'user_firstName'
                },
                {
                    mData: 'user_email'
                },
                {
                    mData: 'user_registrationDate'
                }
            ]
        });
    });
</script>

Mã tập tin PHP

<?php
$host = "127.0.0.1"; //IP of your database
$userName = "root"; //Username for database login
$userPass = ""; //Password associated with the username
$database = "example-database"; //Your database name

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

if(mysqli_connect_errno()){
    echo mysqli_connect_error();
    exit();
}else{
    $selectQuery = "SELECT * FROM `tbl_users` ORDER BY `user_id` ASC";
    $result = mysqli_query($connectQuery,$selectQuery);
    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);

}
?>

Đầu ra

Làm thế nào tìm nạp dữ liệu từ cơ sở dữ liệu trong bảng HTML HTML và hiển thị?

Sử dụng các bước sau để tìm nạp/truy xuất dữ liệu từ cơ sở dữ liệu trong PHP và hiển thị trong bảng HTML:..
Bước 1 - Bắt đầu máy chủ web Apache ..
Bước 2 - Tạo dự án PHP ..
Bước 3 - Thực hiện truy vấn SQL để tạo bảng ..
Bước 4 - Tạo tệp kết nối cơ sở dữ liệu phpmyadmin MySQL ..
Bước 5 - Tạo tệp PHP dữ liệu tìm nạp từ cơ sở dữ liệu ..

Làm thế nào tìm nạp dữ liệu từ cơ sở dữ liệu PostgreSQL trong PHP?

PostgreSQL PHP: Dữ liệu truy vấn..
Đầu tiên, kết nối với cơ sở dữ liệu PostgreSQL bằng cách tạo một đối tượng PDO mới ..
Thứ hai, hãy gọi phương thức truy vấn () của đối tượng PDO. Phương thức Truy vấn () chấp nhận một câu lệnh CHỌN làm đối số. ....
Thứ ba, tìm nạp các hàng tiếp theo từ kết quả bằng cách gọi phương thức Fetch () của đối tượng pdostatement ..

Làm thế nào kết nối cơ sở dữ liệu PostgreSQL với HTML?

Kết nối PostgreSQL với biểu mẫu HTML với LeadsBridge..
Bước 1: Thông tin chính của Bridge.Chọn một tên cho cây cầu của bạn (điều này sẽ chỉ hiển thị bên trong Leadsbridge) ....
Bước 2: Thiết lập nguồn PostgreSQL của bạn.....
Bước 3: Thiết lập điểm đến biểu mẫu HTML của bạn.....
Bước 4: Ánh xạ trường.....
Bước 5: Kiểm tra ..

Làm thế nào để bạn tìm nạp dữ liệu từ cơ sở dữ liệu PostgreSQL trong Python và Hiển thị bảng HTML?

Các bước để thực hiện một truy vấn chọn PostgreSQL từ Python..
Kết nối với PostgreSQL từ Python.....
Xác định một truy vấn chọn postgresql.....
Nhận đối tượng con trỏ từ kết nối.....
Thực thi truy vấn chọn bằng phương thức EXECUTE ().....
Trích xuất tất cả các hàng từ một kết quả.....
Lặp lại mỗi hàng.....
Đóng đối tượng đối tượng con trỏ và đối tượng cơ sở dữ liệu ..

Làm cách nào để hiển thị bảng cơ sở dữ liệu trong HTML?

Hiển thị dữ liệu trong bảng HTML bằng PHP & MySQL..
Kết nối PHP với cơ sở dữ liệu MySQL ..
Chèn dữ liệu vào bảng phpmyadmin ..
Tìm nạp dữ liệu từ bảng MySQL ..
Hiển thị dữ liệu trong bảng HTML ..
Kiểm tra bản thân để chèn dữ liệu ..