Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_stmt_num_rows() Function.
The built-in PHP 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.
The built-in PHP 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
Following below is the syntax to use this function -
mysqli_stmt_num_rows($stmt)
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | stmt(Mandatory) This is an object representing a statement executing an SQL query. |
Return Value
This built-in PHP function returns an integer value indicating the number of rows in the result set returned by the statement.
PHP Version
This built-in PHP function was first introduced in PHP version 5 and works in all the later versions.
Example1
The following below is an example which demonstrates the usage of PHP mysqli_stmt_num_rows() function (in a 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('Kennedy', 27),('Paul', 30),('Justice', 28)"); 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); ?>
Output
When the above code is executed, it will produce the following result -
Table Created..... Records Inserted..... Number of rows in the table: 3
Example2
In an object oriented style the syntax of this function is $stmt->num_rows(); Following is the example of this function in an 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('Kennedy', 27),('Paul', 30),('Justice', 28)"); 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(); ?>
Output
When the above code is executed, it will produce the following result -
Table Created..... Record Inserted..... Number of rows in the table: 3
Example3
Assuming we have created a table named footballers with the following data $minus;
mysql> select * from footballers; +----+------------+------------+---------------+----------------+ | ID | First_Name | Last_Name | Date_Of_Birth | Place_Of_Birth | +----+------------+------------+---------------+----------------+ | 1 | Alex | Iwobi | 1996-05-03 | Nigeria | | 2 | Samuel | Chukwueze | 1999-05-22 | Nigeria | | 3 | Chinedu | Obasi | 1986-06-01 | Nigeria | | 4 | Sone | Aluko | 1989-09-19 | Nigeria | | 5 | Francis | Uzoho | 1998-10-28 | Nigeria | | 6 | Brown | Ideye | 1998-10-10 | Nigeria | +----+------------+------------+---------------+----------------+ 6 rows in set (0.07 sec)
If you try to invoke this built-in PHP 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 footballers"); //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); ?>
Output
When the above code is executed, it will produce the following result below -
Number of rows in the table: 0
Alright guys! This is where we are going to be rounding up with this tutorial post. In our next tutorial, we will study about the PHP mysqli_stmt_param_count() Function.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.