Home > Backend Development > PHP Problem > How to loop query data in php and store it in an array

How to loop query data in php and store it in an array

PHPz
Release: 2023-04-20 13:40:30
Original
1040 people have browsed it

PHP is a very popular programming language that is widely used in the field of web development. When developing web applications, we often need to query data from the database and store the results in an array for processing. This article mainly introduces how to use PHP to query data in a loop and store it in an array.

First, connect to the database. In PHP, connecting to the database can use APIs such as mysqli or PDO. Here we take mysqli as an example to demonstrate:

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

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

// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
Copy after login

Next, write the SQL query statement and execute the query. The query statement can be modified according to actual needs. Here is an example of querying all user data in the user table:

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

We use PHP's query() method to execute the query and store the results in the $result variable. Next, we loop through the query results and store each row of data in an array:

$users = array(); // 创建一个空数组

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $users[] = $row; // 将数据添加到数组中
    }
} else {
    echo "没有数据";
}
Copy after login

In the above code, we first create an empty array $users. Then use PHP's num_rows attribute to check if there is data in the query results. If there is data, we use the fetch_assoc() method to read the data line by line and store each line of data in the $users array. Finally, if there is no data in the query results, we will output a prompt message.

Now, all the data in the query results are stored in the $users array. We can traverse the array to perform appropriate operations:

foreach ($users as $user) {
    echo "姓名:" . $user["name"] . "<br>";
    echo "年龄:" . $user["age"] . "<br>";
    // ...执行其他操作
}
Copy after login

In the above code, we use PHP's foreach loop to traverse the $users array. In each loop, the $user variable is assigned the current array element. We can get the value of the element by accessing the element's key name (such as "name" and "age") and perform the appropriate operation.

Finally, remember to close the database connection:

$conn->close();
Copy after login

To sum up, PHP is a very powerful programming language that can easily query data from the database and store the results in an array processed in. Mastering the above skills can bring more efficient and thoughtful solutions to the field of web development.

The above is the detailed content of How to loop query data in php and store it in an array. For more information, please follow other related articles on the PHP Chinese website!

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