PHP mysqli_num_rows() Function
Definition
The mysqli_num_rows() function returns the number of rows in a result set.
Syntax
PHP mysqli_num_rows() Function has the following syntax.
Object oriented style
int $mysqli_result->num_rows;
Procedural style
int mysqli_num_rows ( mysqli_result $result )
Parameter
Parameter | Description |
---|---|
result | Result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result() |
Return
Returns number of rows in the result set.
If the number of rows is greater than MAXINT, the number will be returned as a string.
Example
The following code returns the number of rows in a result set.
<?php// w w w .j av a2 s . c o m
$con=mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno($con)){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname FROM Persons";
if ($result=mysqli_query($con,$sql)){
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("Result set has %d rows.\n",$rowcount);
mysqli_free_result($result);
}
mysqli_close($con);
?>
Example 2
<?php//from ww w. j ava 2s .c om
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if ($result = $mysqli->query("SELECT Code, Name FROM Country ORDER BY Name")) {
/* determine number of rows result set */
$row_cnt = $result->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
$result->close();
}
$mysqli->close();
?>