English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_stmt_num_rows() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_stmt_num_rows() function returns the number of rows in the statement result set.

Definition and usage

mysqli_stmt_num_rows()The function accepts a statement object as a parameter and returns the number of rows in the result set of the given statement.

Syntax

mysqli_stmt_num_rows($stmt)

Parameter

Serial numberParameters and descriptions
1

stmt(required)

This is the object representing the statement that executes the SQL query.

Return value

The PHP mysqli_stmt_num_rows() function returns an integer value indicating the number of rows returned by the result set of the statement.

PHP version

This function was originally in PHP version5introduced and can be used in all higher versions.

Online example

The following example demonstratesmysqli_stmt_num_rows()Usage of the function (procedural style)-

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   print("Creating table.....\n");
   mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27");
   print("Inserting record.....\n");
   //Reading records
   $stmt = mysqli_prepare($con, "SELECT * FROM Test");
   //Execute statement
   mysqli_stmt_execute($stmt);
   mysqli_stmt_store_result($stmt);
   //Number of rows
   $count = mysqli_stmt_num_rows($stmt);
   print("Number of rows in the table: ".$count."\n");
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Output result

Create table.....
Insert record.....
Number of rows in table: 3

Online example

In the object-oriented style, the syntax of this function is$con->num_rows;Here is an example of this function in an object-oriented style;

<?php
   //Establishing connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   $con -query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   print("Creating table.....\n");
   $con -query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27");
   print("Inserting record.....\n");
   $stmt = $con -prepare("SELECT * FROM Test");
   //Execute statement
   $stmt->execute();
   $stmt->store_result();
   //Number of rows
   $count = $stmt ->num_rows;
   print("Number of rows".$count);
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output result

Create table.....
Insert record.....
Number of rows in table: 3

Online example

Assuming we have created a table named cricketers with the following data;

mysql> select * from cricketers;
+----+------------+------------+---------------+----------------+
| ID | First_Name | Last_Name  | Date_Of_Birth | Place_Of_Birth |
+----+------------+------------+---------------+----------------+
|  1 | Shikhar    | Dhawan     | 1981-12-05    | Delhi          |
|  2 | Jonathan   | Trott      | 1981-04-22    | CapeTown       | 
|  3 | Kumara     | Sangakkara | 1977-10-27    | Matale         |
|  4 | Virat      | Kohli      | 1988-11-05    | Delhi          |
|  5 | Rohit      | Sharma     | 1987-04-30    | Nagpur         |
|  6 | Ravindra   | Jadeja     | 1988-12-06    | Nagpur         |
+----+------------+------------+---------------+----------------+
6 rows in set (0.07 sec)

If you try to call this function directly, because the result has not been stored, it will return0  :

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Reading records
   $stmt = mysqli_prepare($con, "SELECT * FROM cricketers");
   //Execute statement
   mysqli_stmt_execute($stmt);
   print("Number of rows in table: ".mysqli_stmt_num_rows($stmt));
   //End statement
   mysqli_stmt_close($stmt);
   //Close connection
   mysqli_close($con);
?>

Output result

Number of rows in table: 0

PHP MySQLi Reference Manual