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

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

PHPz
PHPzOriginal
2023-04-27 09:07:58657browse

Performing data query operations in PHP is a very common task. Sometimes we need to record the queried data to facilitate subsequent operations. At this time, we need to store the queried data into an array. This article will introduce to you how to store the queried data into an array in PHP.

1. Connect to the database and perform query operations

Before performing data query operations, you need to connect to the database first. Assuming that we already have the correct database connection information, use the following code to connect:

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

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

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

Next, we can use SQL statements to perform data query operations, for example:

$sql = "SELECT id, name, age FROM users";
$result = $conn->query($sql);

The query results will be is stored in the $result variable. It should be noted that if the query result is empty, $result will be an empty result set object.

2. Store the query results in an array

To store the query results in an array, we can use the mysqli_fetch_array function in PHP to obtain the data in the result set row by row and store them Stored in array.

$users = array(); // 定义一个空数组

while ($row = mysqli_fetch_array($result)) {
    $users[] = array(
        'id' => $row['id'],
        'name' => $row['name'],
        'age' => $row['age']
    );
}

The function of the above code is to store each row of data in the query results in the array $users. Each row of data is stored in an associative array, where the key is the name of the column and the key value is the value of the column.

3. Complete code example

The following is a complete example code for connecting to the database, executing queries, and storing the results in an array:

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

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

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

$sql = "SELECT id, name, age FROM users";
$result = $conn->query($sql);

$users = array(); // 定义一个空数组

while ($row = mysqli_fetch_array($result)) {
    $users[] = array(
        'id' => $row['id'],
        'name' => $row['name'],
        'age' => $row['age']
    );
}

// 打印结果
print_r($users);

// 关闭连接
$conn->close();

4. Summary

This article introduces you to the method of storing the queried data into an array in PHP, that is, using the mysqli_fetch_array function to obtain the query results row by row and store them in the array. It should be noted that when performing database operations, you must pay attention to the issue of SQL injection vulnerabilities to ensure the security of the program.

The above is the detailed content of How to query data in php and store it in an array. 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