Home  >  Article  >  Backend Development  >  How to get the number of rows of query results in PHP database learning?

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

WBOY
WBOYOriginal
2021-10-27 18:47:214054browse

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!

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

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 the mysqli_num_rows() function, then let’s take a look at the mysqli_num_rows() function Knowledge.

<strong><span style="font-size: 20px;">mysqli_num_rows()</span></strong> 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 than PHP_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:

<?php
    $host     = &#39;localhost&#39;;
    $username = &#39;root&#39;;
    $password = &#39;root&#39;;
    $dbname   = &#39;test&#39;;
    $mysql    = new Mysqli($host, $username, $password, $dbname);
    if($mysql -> connect_errno){
        die(&#39;数据库连接失败:&#39;.$mysql->connect_errno);
    }else{
        $sql    = &#39;select name,sex,age from user&#39;;     // SQL 语句
        $result = $mysql -> query($sql);               // 执行上面的 SQL 语句
        $num    = $result -> num_rows;                 // 获取查询结果的行数
        $mysql -> close();
    }
    echo &#39;一共查询到 &#39;.$num.&#39; 条记录。&#39;;
?>

Output result:

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

The above example is written in object-oriented way. Let’s take a look at the process-oriented way of writing:

<?php
    $host     = &#39;localhost&#39;;
    $username = &#39;root&#39;;
    $password = &#39;root&#39;;
    $dbname   = &#39;test&#39;;
    $link     = @mysqli_connect($host, $username, $password, $dbname);
    if($link){
    
        $sql    = &#39;select name,sex,age from user&#39;;  // SQL 语句
        $result = mysqli_query($link, $sql);        // 执行 SQL 语句,并返回结果
        $num    = mysqli_num_rows($result);         // 获取查询结果的行数
        mysqli_close($link);
    }else{
        echo &#39;数据库连接失败!&#39;;
    }
    echo &#39;一共查询到 &#39;.$num.&#39; 条记录。&#39;;
?>

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.

<strong><span style="font-size: 20px;">mysqli_fetch_object()</span></strong> 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:

<?php
    $host     = &#39;localhost&#39;;
    $username = &#39;root&#39;;
    $password = &#39;root&#39;;
    $dbname   = &#39;test&#39;;
    $mysql    = new Mysqli($host, $username, $password, $dbname);
    if($mysql -> connect_errno){
        die(&#39;数据库连接失败:&#39;.$mysql->connect_errno);
    }else{
        $sql = &#39;select name,sex,age from user&#39;;     // SQL 语句
        $result = $mysql -> query($sql);            // 执行上面的 SQL 语句
        if($result){
            while($obj = $result -> fetch_object()){
                printf(&#39;姓名:%s,性别:%s,年龄:%s <br>&#39;, $obj->name,$obj->sex,$obj->age);
            }
        }
        $mysql -> close();
    }
?>

Output result:

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

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!

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