search
HomeBackend DevelopmentPHP ProblemExplore 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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),