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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_num_rows() Function Usage and Example

PHP MySQLi Reference Manual

mysqli_num_rows() function the number of rows in the result set.

Definition and usage

A PHP result object (mysqli_result class) represents the MySQL result returned by a SELECT or DESCRIBE or EXPLAIN query.

mysqli_num_rows() function takes a result object as a parameter and retrieves the number of rows returned by the given result.

Syntax

mysqli_num_rows($result);

Parameter

Serial numberParameters and descriptions
1

result (required)

This is the identifier representing the result object.

Return value

The PHP mysqli_num_rows() function returns an integer value representing the number of rows in the given result object/record count.

PHP version

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

Online example

The following examples demonstrate:mysqli_num_rows();Function usage (procedural style), returns the number of rows in the result set:

<?php
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   mysqli_query($con, "CREATE TABLE myplayers(ID INT, First_Name VARCHAR;255), Last_Name VARCHAR;255), Place_Of_Birth VARCHAR;255), Country VARCHAR;255});
   print("Create table.....\n");
   mysqli_query($con, "INSERT INTO myplayers values(1, 'Sikhar', 'Dhawan', 'Delhi', 'India')});
   mysqli_query($con, "INSERT INTO myplayers values(2, 'Jonathan', 'Trott', 'CapeTown', 'SouthAfrica')});
   mysqli_query($con, "INSERT INTO myplayers values(3, 'Kumara', 'Sangakkara', 'Matale', 'Srilanka')");
   print("Insert records.....\n");
   //Retrieve the content of the table
   $result = mysqli_query($con, "SELECT * FROM myplayers");
   //Number of rows
   $count = mysqli_num_rows($result);
   print("Number of rows in the result: ".$count);
   //End statement
   mysqli_free_result($result);
   //Close connection
   mysqli_close($con);
?>

Output result

Create table.....
Insert records.....
Number of rows in the result: 3

Online example

In object-oriented style, the syntax of this function is$result-> num_rows;.The following is an example of this function in object-oriented style;

<?php
   //Establish connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
   $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27);
   print("Create table.....\n");
   $stmt = $con -> prepare("SELECT * FROM Test WHERE Name in(?, ?)");
   $stmt -> bind_param("ss", $name1, $name2);
   $name1 = 'Raju';
   $name2 = 'Rahman';
   //Execute statement
   $stmt->execute();
   //Search results
   $result = $stmt->get_result();
   //Number of rows in the result
   $count = $result->num_rows;
   print("Number of rows in the result: ".$count);
   //End statement
   $stmt->close();
   //Close connection
   $con->close();
?>

Output result

Create table.....
Number of rows in the result: 2

PHP MySQLi Reference Manual