Database query and HTML integration

WBOY
Release: 2024-04-09 14:48:01
Original
471 people have browsed it

You can integrate database query results into HTML pages by following these steps: Establish a database connection. Execute the query and store the results. Loop through the query results and display them in HTML elements.

Database query and HTML integration

Integrate database queries with HTML using PHP

Integrating database query results and HTML pages allows you to create dynamic and interactive web applications. This article walks you through the steps to do this using PHP and provides a practical example to illustrate the process.

Step 1: Establish database connection

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
Copy after login

Step 2: Execute query

To get data, use mysqli_query() Function executes query.

$sql = "SELECT * FROM users";
$result = $conn->query($sql);
Copy after login

Step 3: Get the query results

To iterate over the query results, use the mysqli_fetch_assoc() function. It returns an associative array containing key-value pairs.

while ($row = $result->fetch_assoc()) {
    echo "{$row['id']}: {$row['name']}<br>";
}
Copy after login

Practical case: display user list

The following example shows how to query the user list from the database into an HTML table:

index.php

<!DOCTYPE html>
<html>
<head>
    <title>用户列表</title>
</head>
<body>
    <h1>用户列表</h1>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>姓名</th>
            </tr>
        </thead>
        <tbody>
            <?php
            include 'db_connect.php';

            $sql = "SELECT * FROM users";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td></tr>";
                }
            } else {
                echo "<tr><td colspan='2'>没有用户</td></tr>";
            }
            ?>
        </tbody>
    </table>
</body>
</html>
Copy after login

db_connect.php

// 数据库连接信息
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
Copy after login

The above is the detailed content of Database query and HTML integration. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!