The mysqli_num_rows() function is an inbuilt function in PHP which is used to return the number of rows present in the result set. It is generally used to check if data is present in the database or not. To use this function, it is mandatory to first set up the connection with the MySQL database.
Syntax:
mysqli_num_rows ( $result );
Parameters: This function accepts single parameter $result (where $result is a MySQL query set up using mysqli_query()). It is a mandatory parameter and represents the result set returned by a fetch query in MySQL.
Return Value: It returns the number of rows present in a result set. If no rows match the given criteria then it returns false instead.
Consider there is a table named geek in a MySQL database named Geeks. Below is the description of the table geek.
| Username | Password |
|---|---|
| Geek1 | Pass1 |
| Geek2 | Pass2 |
| Geek3 | Pass3 |
| Geek4 | Pass4 |
Below programs illustrate the mysqli_num_rows() function in PHP.
<?php // Setting up connection with database Geeks $connection = mysqli_connect("localhost", "root", "", "Geeks"); // Check connection if (mysqli_connect_errno()) { echo "Database connection failed."; } // query to fetch Username and Password from // the table geek $query = "SELECT Username, Password FROM geek"; // Execute the query and store the result set $result = mysqli_query($connection, $query); if ($result) { // it return number of rows in the table. $row = mysqli_num_rows($result); if ($row) { printf("Number of row in the table : " . $row); } // close the result. mysqli_free_result($result); } // Connection close mysqli_close($connection); ?> |
Output:
Number of row in the table : 4
Recommended Posts:
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- D3.js | d3.set.add() Function
- PHP | Ds\Set add() Function
- p5.js | nf() Function
- p5.js | nfs() Function
- PHP | Ds\Set first() Function
- PHP | Ds\Set last() Function
- p5.js | nfc() function
- p5.js | nfp() Function
- PHP | Ds\Map get() Function
- p5.js | arc() Function
- PHP | each() Function
- p5.js | box() Function
- D3.js | d3.map.set() Function
- PHP | pos() Function
- CSS min() Function
- D3.js | d3.sum() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : ArkadyutiBanerjee


