Home  >  Article  >  Backend Development  >  php output showing query results

php output showing query results

WBOY
WBOYOriginal
2023-05-06 22:18:09795browse

When developing with PHP, you often need to query data from the database and display it on the user interface. This article will introduce how PHP queries data from the database and outputs and displays the query results.

First, you need to connect to the database. This can be done by using PHP's built-in mysqli or PDO extensions. The following is sample code to connect to a MySQL database using the mysqli extension:

// 从配置文件中获取数据库连接信息
$db_host = "localhost";
$db_user = "username";
$db_pass = "password";
$db_name = "database";

// 连接数据库
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

if (!$conn) {
    die("连接数据库失败:" . mysqli_connect_error());
}

After connecting to the database, you can use SQL statements to query data from the database table. The following is a sample code to query data with specific conditions:

// 查询具有特定条件的数据
$sql = "SELECT * FROM users WHERE age > 18";

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // 遍历查询结果
    while ($row = mysqli_fetch_assoc($result)) {
        echo "姓名:" . $row["name"] . ", 年龄:" . $row["age"];
    }
} else {
    echo "未找到符合条件的数据";
}

In the above sample code, the SQL statement gets all users in the users table who are older than 18 years old. If the query result contains some rows, iterate over the query result and output the data. If no matching row is found, a data not found message is output.

Here, the mysqli_query function is used to execute SQL statements. If the execution is successful, it returns a MySQL result object. You can then use the mysqli_num_rows function to get the number of rows in the query results. If the query results contain any rows, you can use the mysqli_fetch_assoc function to fetch the data for each row as an associative array.

Next, you can use various HTML elements to format and render the query results. Here is some sample code:

<!-- HTML 表格中显示查询结果 -->
<table>
    <thead>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
    </thead>
    <tbody>
        <?php
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr><td>" . $row["name"] . "</td><td>" . $row["age"] . "</td></tr>";
        }
        ?>
    </tbody>
</table>

<!-- 使用 Bootstrap 样式 -->
<div class="card">
    <div class="card-body">
        <?php
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<p class="card-text">姓名:" . $row["name"] . ",年龄:" . $row["age"] . "</p>";
        }
        ?>
    </div>
</div>

With the above sample code, you can format the query results using HTML elements such as tables or cards and render them on the user interface.

It should be noted that it is necessary to iterate over the query results and output the data of each row to the user interface. You must also ensure that the query results are not empty before using them. In this example, the mysqli_num_rows function is used to detect how many rows the query result contains.

In summary, PHP can connect to a database by using mysqli or PDO extensions and query data from the database using SQL statements. Once you have the query results, you can use various HTML elements to format and render it. Finally, be sure to do the necessary checks to ensure that the query result is non-empty, and iterate over it to output the data to the user interface.

The above is the detailed content of php output showing query results. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:php set urban areaNext article:php set urban area