Home  >  Article  >  Backend Development  >  How to convert query results into array in php (three methods)

How to convert query results into array in php (three methods)

PHPz
PHPzOriginal
2023-04-04 17:17:241054browse

In PHP, query results are sometimes returned in different forms, which may be an object or an array. Sometimes the result we want is an array. Therefore, here are several methods of converting query results into arrays to make the query results more in line with our needs.

  1. fetch_assoc() method

fetch_assoc() method returns an associative array, where the key name is the field name in the database, and the key value is the corresponding one in the record value. The fetch_assoc() method returns a row of data from the query result. Calling this method once can only return one row. If you want to read other rows, you need to call this method again.

The sample code is as follows:

// 假设 $conn 是 MySQL 数据库连接对象
$sql = "SELECT id, name, age FROM users";
$result = $conn->query($sql);

// fetch_assoc 方法返回一个关联数组
while ($row = $result->fetch_assoc()) {
    echo $row['id'] , $row['name'] , $row['age'];
}
  1. fetch_array() method

fetch_array() method returns an array that contains both numeric indexes and associative indexes. It is similar to a combination of an associative array and an indexed array. The numeric index corresponds to the index of the column in the query result, and the associated index corresponds to the field name in the database. This method is similar to the fetch_assoc() method, except that the array returned is different.

The sample code is as follows:

// 假设 $conn 是 MySQL 数据库连接对象
$sql = "SELECT id, name, age FROM users";
$result=$conn->query($sql);

// fetch_array 方法返回一个包含关联和数字索引的数组
while ($row = $result->fetch_array()) {
    echo $row['id'] , $row['name'] , $row['age'];
}
  1. fetch_all() method

fetch_all() method returns a full result set. This method requires the mysqli extended version It can only be used with 5.3.0 or above, and only supports the use of MySQL extended driver. The returned result is a two-dimensional array, each sub-array is a row of data, and the elements in the sub-array correspond to each field in the row of records. Using this method can simplify the code.

The sample code is as follows:

// 假设 $conn 是 MySQL 数据库连接对象
$sql = "SELECT id, name, age FROM users";
$result = $conn->query($sql);

// fetch_all 方法返回一个二维数组
$data = $result->fetch_all();
foreach ($data as $row) {
    echo $row[0] , $row[1], $row[2];
}

Summary

The fetch_assoc() method can be used to obtain an associative array with the field name as the key name, and the fetch_array() method can be used to return an An array containing both numeric indexes and associated indexes, while the fetch_all() method can directly return a two-dimensional array of the entire result set. Depending on the specific situation, choosing to use different methods to obtain the query result array can help us operate the data more conveniently.

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