Home >Backend Development >PHP Problem >Explore how PHP converts query results into an array
For developers who use PHP to perform database queries, converting query results into arrays may be one of the common tasks, because arrays are widely used in PHP to facilitate data processing and operations. In this article, we will explore several methods on how to convert query results into an array.
1. Use the mysqli_fetch_array function
The mysqli_fetch_array function is a convenient way to convert query results into an array. This function requires two parameters: the query result object and the return type. The return type parameter has a default value and can be omitted if you do not wish to change the return type. Here is a basic example:
$query = "SELECT * FROM my_table"; $result = mysqli_query($conn, $query); while($row = mysqli_fetch_array($result)){ $my_array[] = $row; }
In the above code, we first execute the query and store the results in the $result variable. We then use a while loop to iterate through each row in the result set and use mysqli_fetch_array to convert the current row to an array and add it to the $my_array array. Finally, when all the rows have been converted to an array, we can use the array for whatever operations we need.
2. Use the mysqli_fetch_assoc function
The mysqli_fetch_assoc function is another way to convert query results into an array. This function returns the current row as an associative array with column names as keys. Here is an example:
$query = "SELECT * FROM my_table"; $result = mysqli_query($conn, $query); while($row = mysqli_fetch_assoc($result)){ $my_array[] = $row; }
In the above code, unlike the mysqli_fetch_array function, we use the mysqli_fetch_assoc function. In this way, we obtain an associative array. The remaining steps are the same as method 1, and the results are ultimately placed in an array.
3. Use the mysqli_fetch_object function
The mysqli_fetch_object function is another way to convert query results into an array. This function returns the current row as an object, where the object's property names correspond to the names of each field. Here is an example:
$query = "SELECT * FROM my_table"; $result = mysqli_query($conn, $query); while($row = mysqli_fetch_object($result)){ $my_array[] = $row; }
In the above code, what is different from the first two methods is that we use the mysqli_fetch_object function. You can use this function if you want to convert the result to an object instead of an array. The attribute names in the object correspond to the names of each fields.
4. Use the PDOStatement::fetch function
When using PDO to query, we can use the PDOStatement::fetch function to convert the query results into an array. Like the mysqli_fetch_array function, this function requires two parameters: the return type and an optional parameter $cursor_orientation. Here is a basic example:
$query = "SELECT * FROM my_table"; $pdo_stmt = $pdo->query($query); while($row = $pdo_stmt->fetch(PDO::FETCH_ASSOC)){ $my_array[] = $row; }
In the above code, we first execute the query and store the results in the $pdo_stmt variable. We then use a while loop to iterate through each row in the result set and use the PDOStatement::fetch function to return the current row as an associative array and add it to the $my_array array. Finally, when all the rows have been converted to an array, we can use the array for whatever operations we need.
Summary
In this article, we explored four ways to convert query results into an array. Whether you are querying using mysqli or PDO, you can use either method. Depending on your specific needs, choose the appropriate method for result conversion. No matter which method you choose, make sure the result set is not empty before using it, otherwise a fatal error will result.
The above is the detailed content of Explore how PHP converts query results into an array. For more information, please follow other related articles on the PHP Chinese website!