Current Location: Home> Latest Articles> How to Efficiently Collect and Display Data Using PHP and Database

How to Efficiently Collect and Display Data Using PHP and Database

M66 2025-06-20

How to Efficiently Collect and Display Data Using PHP and Database

Introduction:
In the information age, data collection and display have become crucial parts of modern applications. PHP, as a popular development language, can efficiently handle data collection and display when combined with a database. This article will provide a detailed guide on how to use PHP and a database to accomplish this task, along with complete code examples to help developers get started quickly.

1. Database Configuration and Connection

First, we need to configure the database connection and communicate with the database using PHP. In PHP, you can use the MySQLi or PDO extension to achieve this. The following example shows how to connect to a database using the MySQLi extension:

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "database_name";

// Create database connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

2. Data Collection

Once connected to the database, we can execute SQL queries to collect data. The following code shows how to query the database and fetch some data:

<?php
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Age: " . $row["age"] . "<br>";
    }
} else {
    echo "No data found";
}
$conn->close();
?>

3. Data Display

After fetching the data, we can use HTML and PHP to display it. The following is a simple example of displaying the data in a table format:

<!DOCTYPE html>
<html>
<head>
    <title>Data Display</title>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
        }
        th, td {
            padding: 8px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
    </style>
</head>
<body>
    <table>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Age</th>
        </tr>
        <?php
        while($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>" . $row["id"] . "</td>";
            echo "<td>" . $row["name"] . "</td>";
            echo "<td>" . $row["age"] . "</td>";
            echo "</tr>";
        }
        ?>
    </table>
</body>
</html>

Conclusion

Through the steps above, we have successfully demonstrated how to collect and display data using PHP and a database. Whether it is data retrieval or display, the process can be done efficiently. We hope that the examples and explanations in this article will help you better understand how to use PHP and a database for data collection and display. If you have any questions or thoughts, feel free to reach out for further discussions.