How to query multiple items in php and turn them into arrays

PHPz
Release: 2023-04-26 10:45:28
Original
733 people have browsed it

When using PHP programming, it often involves querying multiple pieces of data from the database. After querying multiple pieces of data, we sometimes need to store the data in an array for subsequent operations. This article will introduce in detail how to convert the multiple pieces of queried data into an array.

1. Use the mysql_fetch_array() function to convert to an array

The mysql_fetch_array() function obtains a row from the result set as an associative array, a numeric array, or both. It can be easily converted into an array. Query results are converted into arrays.

The sample code is as follows:

//连接数据库
$link = mysql_connect('主机名', '用户名', '密码');
mysql_select_db('数据库名');

//查询数据
$sql = "SELECT * FROM table";
$result = mysql_query($sql);

//遍历结果集
while ($row = mysql_fetch_array($result)){
    $arr[] = $row;//将每行数据添加到数组
}

//关闭连接
mysql_close($link);
Copy after login

In the above code, use the mysql_fetch_array() function to sequentially retrieve each row of data from the query results and save it through the array $arr[].

2. Use the mysqli_fetch_all() function to convert to an array

The mysqli_fetch_all() function will return all rows in the query result set as an associative array or numeric array, which is more efficient than using the mysql_fetch_array() function. Efficient.

The sample code is as follows:

//创建连接
$conn = mysqli_connect("localhost", "username", "password", "database");

//检查连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

//查询数据
$sql = "SELECT * from table";
$result = mysqli_query($conn, $sql);

//将查询结果转换为数组
$myArray = mysqli_fetch_all($result,MYSQLI_ASSOC);

//输出查询结果
foreach ($myArray as $key=>$value) {
    echo "Row ".$key." has ".count($value)." columns: ";
    foreach ($value as $inner_key=>$inner_value) {
        echo $inner_key." -> ".$inner_value.", ";
    }
    echo "\n";
}

//关闭连接
mysqli_close($conn);
Copy after login

In the above code, we first use the mysqli_fetch_all() function to convert the query results into an array, and then use the foreach loop to traverse the array and output the detailed information of each query result. .

Summary

By using the mysql_fetch_array() function or mysqli_fetch_all() function, we can easily convert multiple pieces of queried data into arrays, which facilitates our subsequent processing. When using it, you need to pay attention to choosing the appropriate function and the correct usage method.

The above is the detailed content of How to query multiple items in php and turn them into arrays. 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