Home  >  Article  >  Backend Development  >  How to put query results into an array using PHP

How to put query results into an array using PHP

PHPz
PHPzOriginal
2023-04-20 13:52:37960browse

PHP is a powerful programming language with a wide range of applications. When we use PHP to query the database, we usually need to put the query results into an array for subsequent operations. This article will introduce how to use PHP to put query results into an array.

1. Use the mysql_fetch_array() function to obtain query results

In PHP, we use the mysql_query() function to perform query operations. At the same time, we can use the mysql_fetch_array() function to get the data rows from the query results and return them as an array. This function accepts one parameter - the result set returned after executing the query, so we can place it in a while loop to get multiple rows. The following is the sample code:

$query = mysql_query("SELECT * FROM mytable");
$result = array();
while($row = mysql_fetch_array($query)){
    $result[] = $row;
}

In the above example, we first performed a query operation using the mysql_query() function and saved the result set in the variable $query. Next, we define an array called $result and use a while loop to iterate over the rows in $query. During the loop, we use the mysql_fetch_array() function to get the row data and save it to the $row variable. Next, we add $row to the $result array for subsequent use.

2. Use the mysqli_fetch_array() function to obtain query results

If you are using PHP 5 or higher, you should use the mysqli function instead of the mysql function. The mysqli function provides an improved set of functions and options for MySQL databases. When using mysqli, you can use the mysqli_query() function and mysqli_fetch_array() function to query and obtain results. The following is a sample code for using the mysqli_fetch_array() function to obtain query results:

$query = mysqli_query($connection,"SELECT * FROM mytable");
$result = array();
while($row = mysqli_fetch_array($query)){
    $result[] = $row;
}

In the above example, we use the mysqli_query() function to perform the query operation and save the result set in the variable $query. Next, we define an array called $result and use a while loop to iterate over the rows in $query. During the loop, we use the mysqli_fetch_array() function to get the row data and save it to the $row variable. Finally, we add $row to the $result array for subsequent use.

Summary

Putting query results into a PHP array is a very simple task. We just need to use the mysql_fetch_array() function or mysqli_fetch_array() function to access the result set and place it in a loop. Whether you're using an older or newer version of PHP, you can use these features to easily get MySQL query results and place them into an array for subsequent processing.

The above is the detailed content of How to put query results into an array using PHP. 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