Table of Contents
1. Use fetch_assoc()
2. Using fetch_row()
3. Using fetch_all()
Summary
Home Backend Development PHP Problem A brief analysis of how PHP returns query results in the form of an array

A brief analysis of how PHP returns query results in the form of an array

Apr 04, 2023 am 09:15 AM

In PHP, querying the database is one of the very common tasks. The results of the query may involve multiple rows and columns of data. At this time, we need to process the query results into an array to facilitate subsequent processing.

This article will introduce several methods of returning query results in array form.

1. Use fetch_assoc()

The fetch_assoc() function is a function in the MySQLi class, which is used to extract query results from resource objects and merge them into an associative array. Associative arrays use column names as keys and data as values, making it easy to process data.

The following is an example of using the fetch_assoc() function:

// 连接MySQL数据库
$con = mysqli_connect("localhost","root","","test");

// 查询
$result_set = mysqli_query($con, "SELECT * FROM products");

// 结果形成数组
while ($row = mysqli_fetch_assoc($result_set)) {
    $result_array[] = $row;
}

// 输出数组
print_r($result_array);
Copy after login

2. Using fetch_row()

The fetch_row() function is also a function in the MySQLi class for Extract query results from resource objects and merge them into an index array. The index array is indexed by the column number in the data table and the data as the value.

The following is an example of using the fetch_row() function:

// 连接MySQL数据库
$con = mysqli_connect("localhost","root","","test");

// 查询
$result_set = mysqli_query($con, "SELECT * FROM products");

// 结果形成数组
while ($row = mysqli_fetch_row($result_set)) {
    $result_array[] = $row;
}

// 输出数组
print_r($result_array);
Copy after login

3. Using fetch_all()

The fetch_all() function is a new function in the MySQLi class. Use Extract the query results from the resource object and merge them into a two-dimensional array. A two-dimensional array has row and column numbers as subscripts and data as values.

The following is an example of using the fetch_all() function:

// 连接MySQL数据库
$con = mysqli_connect("localhost","root","","test");

// 查询
$result_set = mysqli_query($con, "SELECT * FROM products");

// 结果形成数组
$result_array = mysqli_fetch_all($result_set, MYSQLI_ASSOC);

// 输出数组
print_r($result_array);
Copy after login

Summary

The above are the three basic methods for forming arrays from query results, using fetch_assoc(), fetch_row respectively. () and fetch_all() functions. Among them, the fetch_assoc() function is suitable for most situations because it provides the most convenient processing method. The fetch_row() function and fetch_all() function are mainly used for processing in specific situations.

In practical applications, arrays can also be processed according to actual needs, such as using array_map() and array_column() functions to operate and extract array elements, etc.

The above is the detailed content of A brief analysis of how PHP returns query results in the form of an array. For more information, please follow other related articles on the PHP Chinese website!

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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24