PHP mysqli_stmt_num_rows() Function
Definition and Usage
The mysqli_stmt_num_rows() 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)
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
stmt(Mandatory) This is an object representing a statement executing an SQL query. |
Return Values
PHP mysqli_stmt_num_rows() function returns an integer value indicating the number of rows in the resultset returned by the statement.
PHP Version
This function was first introduced in PHP Version 5 and works works in all the later versions.
Example
Following example demonstrates the usage of the mysqli_stmt_num_rows() function (in procedural style) −
<?php
$con = mysqli_connect("localhost", "root", "password", "mydb");
mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
print("Table Created.....\n");
mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
print("Records Inserted.....\n");
//Reading records
$stmt = mysqli_prepare($con, "SELECT * FROM Test");
//Executing the 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");
//Closing the statement
mysqli_stmt_close($stmt);
//Closing the connection
mysqli_close($con);
?>
This will produce following result −
Table Created..... Records Inserted..... Number of rows in the table: 3
Example
In object oriented style the syntax of this function is $con->num_rows; Following is the example of this function in object oriented style $minus;
<?php
//Creating a connection
$con = new mysqli("localhost", "root", "password", "mydb");
$con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
print("Table Created.....\n");
$con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
print("Records Inserted.....\n");
$stmt = $con -> prepare( "SELECT * FROM Test");
//Executing the statement
$stmt->execute();
$stmt->store_result();
//Number of rows
$count = $stmt ->num_rows;
print("Rows affected ".$count);
//Closing the statement
$stmt->close();
//Closing the connection
$con->close();
?>
This will produce following result −
Table Created..... Records Inserted..... Number of rows in the table: 3
Example
Assume we have created a table named cricketers with the following data $minus;
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 invoke this function directly, since the results haven't stored yet, it returns 0 −
<?php
$con = mysqli_connect("localhost", "root", "password", "mydb");
//Reading records
$stmt = mysqli_prepare($con, "SELECT * FROM cricketers");
//Executing the statement
mysqli_stmt_execute($stmt);
print("Number of rows in the table: ".mysqli_stmt_num_rows($stmt));
//Closing the statement
mysqli_stmt_close($stmt);
//Closing the connection
mysqli_close($con);
?>
This will produce following result −
Number of rows in the table: 0