Gets the number of rows in a result (original) (raw)
mysqli_result::$num_rows
mysqli_num_rows
(PHP 5, PHP 7)
mysqli_result::$num_rows -- mysqli_num_rows — Gets the number of rows in a result
Description
Object oriented style
intmysqli_result->num_rows;
Procedural style
The behaviour of mysqli_num_rows() depends on whether buffered or unbuffered result sets are being used. For unbuffered result sets, mysqli_num_rows() will not return the correct number of rows until all the rows in the result have been retrieved.
Return Values
Returns number of rows in the result set.
Note:
If the number of rows is greater than
PHP_INT_MAX
, the number will be returned as a string.
Examples
Example #1 Object oriented style
`<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (
result=result = result=mysqli->query("SELECT Code, Name FROM Country ORDER BY Name")) {/* determine number of rows result set / rowcnt=row_cnt = rowcnt=result->num_rows;printf("Result set has %d rows.\n", $row_cnt);/ close result set /
$result->close();
}/ close connection */
$mysqli->close();
?> `
Example #2 Procedural style
`<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
if (
result=mysqliquery(result = mysqli_query(result=mysqliquery(link, "SELECT Code, Name FROM Country ORDER BY Name")) {/* determine number of rows result set / rowcnt=mysqlinumrows(row_cnt = mysqli_num_rows(rowcnt=mysqlinumrows(result);printf("Result set has %d rows.\n", $row_cnt);/ close result set /
mysqli_free_result($result);
}/ close connection */
mysqli_close($link);
?> `
The above examples will output:
See Also
- mysqli_affected_rows() - Gets the number of affected rows in a previous MySQL operation
- mysqli_store_result() - Transfers a result set from the last query
- mysqli_use_result() - Initiate a result set retrieval
- mysqli_query() - Performs a query on the database