Home > Backend Development > PHP Problem > How to convert recordset to array in php (three methods)

How to convert recordset to array in php (three methods)

PHPz
Release: 2023-04-19 11:26:57
Original
844 people have browsed it

The record set in PHP refers to the result set obtained through database query operation, which is usually used to obtain data in a data table. When using recordsets, we often need to convert them into array format to facilitate data processing.

Below we will give several methods to convert recordsets into arrays in PHP.

Method 1: Use the mysqli_fetch_all() function

mysqli_fetch_all() is a built-in function in PHP, used to get all rows of data from the result set and return a two-dimensional array.

Sample code:

$query = "SELECT * FROM mytable";
$result = mysqli_query($conn, $query);
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);
print_r($data);
Copy after login

In the above code, $conn is the connection handle returned when connecting to the database, $query is the query statement, $result is the query result set, and $data is what we want to get array.

Method 2: Use a while loop and use the array to add

We can add the result set to the array row by row in the while loop. Since each addition operation is performed at the end of the array, the generated array is also a two-dimensional array.

Sample code:

$query = "SELECT * FROM mytable";
$result = mysqli_query($conn, $query);
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}
print_r($data);
Copy after login

In the above code, $row is the data of each row, and $row is added to the $data array each time.

Method 3: Use PDOStatement::fetchAll() function

PDOException::fetchAll() is a function extended by PHP PDO, used to obtain all data records from the result set and return an array.

Sample code:

$query = "SELECT * FROM mytable";
$result = $pdo->query($query);
$data = $result->fetchAll(PDO::FETCH_ASSOC);
print_r($data);
Copy after login

In the above code, $pdo is the PDO object established through PDO and the database, $query is the query statement, $result is the result set of the query, and $data is our The array to be obtained.

Summary

The above introduces three PHP methods to convert a record set into an array. Among them, the mysqli_fetch_all() and PDOStatement::fetchAll() functions are PHP built-in functions, while the while loop is Achieved by manual iteration. Each of these three methods has its own advantages and disadvantages. Please choose the appropriate method according to your needs and specific scenarios.

The above is the detailed content of How to convert recordset to array in php (three methods). 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