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

How to get the number of rows of query results in PHP database

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

Share

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

This article introduces the knowledge of "how to obtain the number of rows of query results in PHP database". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In the previous study, we have learned how to obtain SQL query results, how to execute multiple SQL statements at a time, there is a function in PHP that can obtain the number of rows of query results, that is, how many pieces of data are in the query result set, this function is the mysqli_num_rows () function, then let's take a look at the relevant knowledge of the mysqli_num_rows () function.

Mysqli_num_rows () function

In PHP, if you want to get how many pieces of data are in the result set queried by the SELECT statement, you need to use the mysqli_num_rows () function. First, let's take a look at the grammatical structure of the function:

$mysqli_result-> num_rows

This is the syntax of object-oriented writing, and the following is the syntax of process-oriented writing:

Mysqli_num_rows (mysqli_result $result)

One of the things we need to pay attention to is:

$mysqli_result and $result are the result sets returned using the mysqli_query () function.

The mysqli_num_rows () function is valid only for SELECT statements, and if the number of lines returned is greater than PHP_INI_MAX, the number of lines is returned as a string.

Let's take a look at the use of the mysqli_num_rows () function through an example.

Examples are as follows:

Output result:

The above example is written in an object-oriented way, so let's take a look at the process-oriented method:

The output is the same as the above, and in the above example, the result of querying how many pieces of data there are in the dataset is completed through the mysqli_num_rows () function.

Now I would like to add that when we learn the query results, the output results are all returned in the form of an indexed array or an associative array. I would like to add that the results are returned in the form of objects, which will be achieved through the mysqli_fetch_object () function.

Mysqli_fetch_object () function

The mysqli_fetch_object () function takes a row from the result set and returns it as an object. Its syntax format is as follows:

Mysqli_result::fetch_object ([string $class_name = "stdClass" [, array $params]])

This is object-oriented writing. Let's take a look at the process-oriented writing syntax format as follows:

Mysqli_fetch_object (mysqli_result $result [, string $class_name = "stdClass" [, array $params]])

Among them, it should be noted that:

Mysqli_result represents the result set obtained by the mysqli_query () function

$class_name is expressed as an optional parameter to specify the instantiated class name, set the property, and return

$params is expressed as an optional parameter and is used to specify an array of optional parameters passed to the constructor of $classname.

Next, let's take a look at the example of the mysqli_fetch_object () function that returns the current row in the result set and outputs the value of each field.

Examples are as follows:

Output result:

So we can get a row from the result set through the mysqli_fetch_object () function and return it as an object.

This is the end of the content of "how to get the number of rows of query results in PHP database". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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