Home Backend Development PHP Tutorial 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?

Oct 27, 2021 pm 06:47 PM
mysql php

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:

1

$mysqli_result -> num_rows;

Copy after login

This is the syntax for object-oriented writing. The following is the syntax for process-oriented writing:

1

mysqli_num_rows(mysqli_result $result)

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?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;;

?>

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?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;;

?>

Copy after login

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:

1

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

Copy after login

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

1

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

Copy after login

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?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();

    }

?>

Copy after login

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 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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles