Home>Article>Backend Development> How to get the number of rows of query results in PHP database learning?
In the previous article, I brought you "How to execute multiple SQL commands at once when learning PHP database?"", which introduces in detail the relevant knowledge of executing multiple SQL commands at once in PHP. In this article, we will take a look at how to obtain the number of rows of query results in PHP. I hope it will be helpful to everyone!
In previous studies, we have learned how to obtain SQL query results and how to execute multiple SQL statements at one time. There are functions in PHP that can obtain query results. The number of rows, that is, how many pieces of data are in the query result set, this function is themysqli_num_rows()
function, then let’s take a look at themysqli_num_rows()
function Knowledge.
mysqli_num_rows()
Function
in 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 syntax structure of the function:
$mysqli_result -> num_rows;
This is the syntax for object-oriented writing. The following is the syntax for process-oriented writing:
mysqli_num_rows(mysqli_result $result)
What we need to pay attention to is:
$mysqli_result
and$result
are the result sets returned by using the mysqli_query() function.
mysqli_num_rows()
The function is only valid for SELECT statements. If the number of rows returned is greater thanPHP_INI_MAX
, the number of rows will be converted into a string form return.
Next, let’s take a look at the usage of the mysqli_num_rows() function through an example.
The example is as follows:
connect_errno){ die('数据库连接失败:'.$mysql->connect_errno); }else{ $sql = 'select name,sex,age from user'; // SQL 语句 $result = $mysql -> query($sql); // 执行上面的 SQL 语句 $num = $result -> num_rows; // 获取查询结果的行数 $mysql -> close(); } echo '一共查询到 '.$num.' 条记录。'; ?>
Output result:
The above example is written in object-oriented way. Let’s take a look at the process-oriented way of writing:
The output result is the same as the above result. In the above example, The mysqli_num_rows() function completes the query of how many pieces of data there are in the data set.
Let me add something to you. When we learned the query results before, the output results were all returned in the form of index arrays or associative arrays. Let me add to you the results returned in the form of objects. Then we need to This is achieved through the mysqli_fetch_object() function.
mysqli_fetch_object()
Function
mysqli_fetch_object ()
The function can get a row from the result set and return it in the form of 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]])
What needs to be noted is:
mysqli_result
represents the result set obtained by the mysqli_query() function;
$class_name
Represented as an optional parameter, used to specify the instantiated class name, set attributes and return;
$ params
represents optional parameters, used to specify an array of optional parameters passed to the constructor of $classname.
Next let's look at the mysqli_fetch_object() function through an example. The function returns the current row in the result set and outputs the value of each field.
The example is as follows:
connect_errno){ die('数据库连接失败:'.$mysql->connect_errno); }else{ $sql = 'select name,sex,age from user'; // SQL 语句 $result = $mysql -> query($sql); // 执行上面的 SQL 语句 if($result){ while($obj = $result -> fetch_object()){ printf('姓名:%s,性别:%s,年龄:%s
', $obj->name,$obj->sex,$obj->age); } } $mysql -> close(); } ?>
Output result:
From this we can use the mysqli_fetch_object() function to extract the results from the result set Gets a row and returns it as an object.
If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge.
The above is the detailed content of How to get the number of rows of query results in PHP database learning?. For more information, please follow other related articles on the PHP Chinese website!