PHP converts query results into a two-dimensional array

WBOY
Release: 2023-05-05 20:23:07
Original
479 people have browsed it

php is a popular programming language that is often used to develop web applications. Querying the database is one of the commonly used operations in PHP, and the query results need to be returned in the form of an array. This article will describe how to convert query results into a two-dimensional array.

1. Query results

In PHP, use mysqli or PDO to connect to the database for query operations. The following is a sample code using mysqli query:

$mysqli = new mysqli("localhost", "user", "password", "database"); $sql = "SELECT name, age FROM users"; $result = $mysqli->query($sql);
Copy after login

Sample code using PDO:

$pdo = new PDO("mysql:host=localhost;dbname=database", "user", "password"); $sql = "SELECT name, age FROM users"; $result = $pdo->query($sql);
Copy after login

Whether it is mysqli or PDO, after executing the query statement, a result set object will be returned.

2. Convert the results into an array

There are two ways to convert the query results into an array. One is to use the fetch function that comes with mysqli or PDO to convert row by row; the other The first is to retrieve the entire result set into an array at once.

  1. fetch() method

The fetch method is used to retrieve a row of data from the result set and move the pointer down one row. The fetch() method can be called in a loop to obtain the entire result set. The following is a sample code:

// mysqli示例代码 $rows = array(); while ($row = $result->fetch_assoc()) { $rows[] = $row; } // PDO示例代码 $rows = array(); while ($row = $result->fetch(PDO::FETCH_ASSOC)) { $rows[] = $row; }
Copy after login

The above code uses fetch_assoc() or FETCH_ASSOC to retrieve an associative array. You can also use fetch_array() or FETCH_BOTH to obtain an array containing numbers and associated keys. fetch_object() or FETCH_OBJ returns an object. .

  1. fetch_all() method

The fetch_all() method fetches the entire result set into an array at once. fetch_all() is a function in PDO. In mysqli, you need to use get_result() to obtain the result set object. The following is a sample code:

// mysqli示例代码 $rows = $result->get_result()->fetch_all(MYSQLI_ASSOC); // PDO示例代码 $rows = $result->fetch_all(PDO::FETCH_ASSOC);
Copy after login

The above code takes out the result set at once and stores all the data in the result set in a two-dimensional array in the form of an associative array.

3. Summary

The two methods introduced above each have their own advantages and disadvantages. Using the fetch() method can process the results row by row, which is more flexible, but requires a lot of code to process each row of data. . The fetch_all() method can fetch the entire result at once. The code is simple, but when the amount of data is too large, it may occupy too much memory.

In actual development, you can choose a suitable method according to the actual situation. If the query result set is small, you can use the fetch() method; otherwise, it is recommended to use the fetch_all() method.

The above is the detailed content of PHP converts query results into a two-dimensional 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!