mysqli_stmt $num_rows Property
The mysqli_stmt::$num_rows / mysqli_stmt::num_rows() / mysqli_stmt_num_rows() function is used to get the number of rows buffered in the statement. This function will only work after mysqli_stmt_store_result() is called to buffer the entire result set in the statement handle.
This function returns 0 unless all rows have been fetched from the server.
Syntax
//Object-oriented style $mysqli_stmt->num_rows; mysqli_stmt::num_rows(); //Procedural style mysqli_stmt_num_rows(statement)
Parameters
statement |
Required. For procedural style only: Specify a mysqli_stmt object returned by mysqli_stmt_init(). |
Return Value
Returns an int representing the number of fetched rows. Returns 0 in unbuffered mode unless all rows have been fetched from the server.
Note: If the number of rows is greater than PHP_INT_MAX, the number will be returned as a string.
Example: Object-oriented style
The example below shows the usage of mysqli_stmt::$num_rows property.
<?php //establishing connection to the database $mysqli = new mysqli("localhost", "user", "password", "database"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: ". $mysqli->connect_error; exit(); } //preparing an SQL statement for execution $query = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age"; $stmt = $mysqli->prepare($query); //executing the SQL statement $stmt->execute(); //storing the result in an internal buffer $stmt->store_result(); //getting the number of rows buffered in statement printf("Number of rows: %d \n", $stmt->num_rows); ?>
The output of the above code will be similar to:
Number of rows: 58
Example: Procedural style
The example below shows the usage of mysqli_stmt_num_rows() function.
<?php //establishing connection to the database $mysqli = mysqli_connect("localhost", "user", "password", "database"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: ". mysqli_connect_error(); exit(); } //preparing an SQL statement for execution $query = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age"; $stmt = mysqli_prepare($mysqli, $query); //executing the SQL statement mysqli_stmt_execute($stmt); //storing the result in an internal buffer mysqli_stmt_store_result($stmt); //getting the number of rows buffered in statement printf("Number of rows: %d \n", mysqli_stmt_num_rows($stmt)); ?>
The output of the above code will be similar to:
Number of rows: 58
❮ MySQLi Functions Reference