Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common functions for dealing with SQL query results in PHP

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what are the common functions for processing SQL query results in PHP", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn "what are the common functions for processing SQL query results in PHP".

mysqli_fetch_row() function

The mysqli_fetch_row() function takes a row from the result set and returns it as an indexed array, with the following syntax:

mysqli_result::fetch_row()

This is object-oriented writing, and process-oriented writing is as follows:

mysqli_fetch_row(mysqli_result $result)

Note that mysqli_result and $result represent the result set obtained using the mysqli_query() function.

Next, let's look at the usage of mysqli_fetch_row() function through an example, as follows:

Output:

In the above example, the mysqli_fetch_row() function successfully queries one of the rows of data in the database and returns it as an indexed array. So let's look at the different forms of return.

mysqli_fetch_assoc() function

The mysqli_fetch_assoc() function takes a row from the result set and returns it as an associative array. The syntax for this function is as follows:

mysqli_result::fetch_assoc()

This is how it's written in object-oriented syntax, and here's how it's written in procedure-oriented syntax:

mysqli_fetch_assoc(mysqli_result $result)

Note that mysqli_result and $result represent the result set obtained using the mysqli_query() function.

Next, let's look at the use of mysqli_fetch_assoc() function through an example, as follows:

Output:

From the above example, we successfully retrieve a row of information from the database through the mysqli_fetch_assoc() function and return it as an associative array. We can also control the form of the returned data through functions, so that it can be an indexed array or an associative array or both, in which case we will use the mysqli_fetch_array() function.

mysqli_fetch_array() function

The mysqli_fetch_array() function takes a row from the result set and returns it as an associative array, indexed array, or both, depending on the parameters. Its syntax is as follows:

mysqli_result::fetch_array([int $resulttype = MYSQLI_BOTH])

This is the object-oriented syntax. Here is the procedure-oriented syntax:

mysqli_fetch_array(mysqli_result $result[, int $resulttype = MYSQLI_BOTH])

Among them, it should be noted that:

mysqli_result and $result represent the result set obtained using the mysqli_query() function.

$resulttype is an optional parameter. It is a constant used to set the type of return value. Its value can be MYSQLI_ASSOC, MYSQLI_NUM or MYSQLI_BOTH to indicate different types of return value.

Next, let's look at the usage of mysqli_fetch_array() function through an example, as follows:

Output:

In the above example, we chose to return the data in the form of associative array. We can choose different types of return values through mysqli_fetch_array() function.

mysqli_fetch_all() function

The mysqli_fetch_all() function takes all the data in the result set and returns it as an associative array, indexed array, or both, depending on the parameters. Its syntax is as follows:

mysqli_result::fetch_all([int $resulttype = MYSQLI_NUM])

This is object-oriented writing, and here is process-oriented writing:

mysqli_fetch_all(mysqli_result $result [, int $resulttype = MYSQLI_NUM])

Note that the syntax is the same as mysqli_fetch_array().

mysqli_result and $result represent the result set obtained using the mysqli_query() function.

$resulttype is an optional parameter. It is a constant used to set the type of return value. Its value can be MYSQLI_ASSOC, MYSQLI_NUM or MYSQLI_BOTH to indicate different types of return value.

Let's take a look at the use of mysqli_fetch_all() as an example:

Output:

The above is "PHP SQL query results of the commonly used functions what" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report