Home  >  Article  >  Backend Development  >  A brief analysis of how PHP returns query results in the form of an array

A brief analysis of how PHP returns query results in the form of an array

PHPz
PHPzOriginal
2023-04-04 09:15:00676browse

In PHP, querying the database is one of the very common tasks. The results of the query may involve multiple rows and columns of data. At this time, we need to process the query results into an array to facilitate subsequent processing.

This article will introduce several methods of returning query results in array form.

1. Use fetch_assoc()

The fetch_assoc() function is a function in the MySQLi class, which is used to extract query results from resource objects and merge them into an associative array. Associative arrays use column names as keys and data as values, making it easy to process data.

The following is an example of using the fetch_assoc() function:

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

// 查询
$result_set = mysqli_query($con, "SELECT * FROM products");

// 结果形成数组
while ($row = mysqli_fetch_assoc($result_set)) {
    $result_array[] = $row;
}

// 输出数组
print_r($result_array);

2. Using fetch_row()

The fetch_row() function is also a function in the MySQLi class for Extract query results from resource objects and merge them into an index array. The index array is indexed by the column number in the data table and the data as the value.

The following is an example of using the fetch_row() function:

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

// 查询
$result_set = mysqli_query($con, "SELECT * FROM products");

// 结果形成数组
while ($row = mysqli_fetch_row($result_set)) {
    $result_array[] = $row;
}

// 输出数组
print_r($result_array);

3. Using fetch_all()

The fetch_all() function is a new function in the MySQLi class. Use Extract the query results from the resource object and merge them into a two-dimensional array. A two-dimensional array has row and column numbers as subscripts and data as values.

The following is an example of using the fetch_all() function:

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

// 查询
$result_set = mysqli_query($con, "SELECT * FROM products");

// 结果形成数组
$result_array = mysqli_fetch_all($result_set, MYSQLI_ASSOC);

// 输出数组
print_r($result_array);

Summary

The above are the three basic methods for forming arrays from query results, using fetch_assoc(), fetch_row respectively. () and fetch_all() functions. Among them, the fetch_assoc() function is suitable for most situations because it provides the most convenient processing method. The fetch_row() function and fetch_all() function are mainly used for processing in specific situations.

In practical applications, arrays can also be processed according to actual needs, such as using array_map() and array_column() functions to operate and extract array elements, etc.

The above is the detailed content of A brief analysis of how PHP returns query results in the form of 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