Home  >  Article  >  Backend Development  >  Does php query mysql return an array?

Does php query mysql return an array?

WBOY
WBOYOriginal
2023-05-07 17:45:08562browse

PHP is a scripting language widely used in the field of Web development, while MySQL is a widely used relational database management system. In PHP, we can use the MySQL extension to connect and operate the MySQL database. Among them, when we use the SELECT statement to query data in the MySQL database, we can obtain the query results in a variety of ways, one of which is to return an array.

In PHP, using MySQLi or PDO to operate the MySQL database is the most common method. The following describes how to use these two extensions to query the MySQL database and return arrays.

Using MySQLi extension to query the MySQL database to return an array

When using the MySQLi extension to query the MySQL database, you can use MySQLi's query() method to execute the SELECT statement and obtain the query results. The query() method will return a result set object. We can get the array form of the query results by calling the fetch_all() method.

The sample code is as follows:

// 建立MySQL数据库连接
$conn = new mysqli('localhost', 'root', 'password', 'database');

// 执行SELECT语句并获取查询结果
$result = $conn->query("SELECT * FROM table");

// 将查询结果转化为数组形式
$rows = $result->fetch_all(MYSQLI_ASSOC);

// 输出查询结果数组
foreach ($rows as $row) {
    print_r($row);
}

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

Among them, the parameter MYSQLI_ASSOC of the fetch_all() method means to convert the query results into an associative array form. You can also use MYSQLI_NUM to mean to convert the results into a numeric index array form. Or use MYSQLI_BOTH to return both associative array and numeric index array forms.

Using PDO extension to query the MySQL database returns an array

When using the PDO extension to query the MySQL database, you can use the PDO query() method or prepare() method to query. The query() method will directly execute the SQL statement and return the result set object, while the prepare() method needs to call the execute() method to execute the preprocessed SQL statement and return a prepared statement object. No matter which method is used, the result set object can be converted into an array form by calling the fetchAll() method.

The sample code is as follows:

// 建立MySQL数据库连接
$conn = new PDO('mysql:host=localhost;dbname=database', 'root', 'password');

// 执行SELECT语句并获取查询结果
$result = $conn->query("SELECT * FROM table");

// 将查询结果转化为数组形式
$rows = $result->fetchAll(PDO::FETCH_ASSOC);

// 输出查询结果数组
foreach ($rows as $row) {
    print_r($row);
}

// 关闭MySQL连接
$conn = null;

Among them, the parameter PDO::FETCH_ASSOC of the fetchAll() method means converting the result set object into an associative array form. You can also use PDO::FETCH_NUM to represent the result. Convert to numeric index array form, or use PDO::FETCH_BOTH to return both associative array and numeric index array form.

Summary

The above describes how to use MySQLi and PDO extensions to query the MySQL database and return query results in the form of an array. Different extension functions have different calling methods, but their essence is to return the query result set object and convert it into an array form. In actual development, different extension functions can be selected according to specific circumstances to query the MySQL database.

The above is the detailed content of Does php query mysql return 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