Home  >  Article  >  Backend Development  >  How to loop query and output data in php (code example)

How to loop query and output data in php (code example)

PHPz
PHPzOriginal
2023-04-11 10:42:571393browse

PHP is a general open source server-side scripting language with powerful data processing capabilities and is widely used in Web development. In web development, it is often necessary to perform data query and loop output. Here we will introduce how to use PHP to perform loop query and output data.

1. Connect to the database

First, we need to connect to the database in PHP to obtain the data that needs to be queried and output. Using PHP's built-in mysqli function, you can easily connect to the database.

The sample code is as follows:

//连接MySQL数据库
$con = mysqli_connect("localhost","root","password","database_name");

//判断连接是否成功
if (!$con){
    die('Could not connect: ' . mysqli_error());
}

2. Query the database

After successfully connecting to the database, we need to use SQL statements for data query. Here we take querying the user table as an example and use the SELECT statement to query all user information.

The sample code is as follows:

//查询所有用户信息
$sql = "SELECT * FROM users";

//执行查询
$result = mysqli_query($con,$sql);

//判断是否查询成功
if (!$result) {
    die('Error: ' . mysqli_error($con));
}

3. Loop output data

After querying the data, we need to loop it out. Use PHP's while loop to iterate through the query results and output each piece of data.

The sample code is as follows:

//循环输出查询结果
while($row = mysqli_fetch_assoc($result)) {
    echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Age: " . $row["age"] . "<br>";
}

In the above code, the mysqli_fetch_assoc function is used to obtain a piece of data from the query result, and then echo is used to output the data. Repeat this operation until all data is output.

4. Complete code example

//连接MySQL数据库
$con = mysqli_connect("localhost","root","password","database_name");

//判断连接是否成功
if (!$con){
    die('Could not connect: ' . mysqli_error());
}

//查询所有用户信息
$sql = "SELECT * FROM users";

//执行查询
$result = mysqli_query($con,$sql);

//判断是否查询成功
if (!$result) {
    die('Error: ' . mysqli_error($con));
}

//循环输出查询结果
while($row = mysqli_fetch_assoc($result)) {
    echo "ID: " . $row["id"] . " - Name: " . $row["name"] . " - Age: " . $row["age"] . "<br>";
}

//断开数据库连接
mysqli_close($con);

In the above code, we first connect to the MySQL database, then query all user information, and finally use a loop statement to output all query results.

Summary

The above is how to use PHP to query and output data in a loop. In web development, data processing is an indispensable part. Proficient in PHP's data processing capabilities can allow us to complete development work more efficiently.

The above is the detailed content of How to loop query and output data in php (code example). 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