PHP: Hypertext Preprocessor (original) (raw)
mysqli_stmt::$num_rows
mysqli_stmt::num_rows
mysqli_stmt_num_rows
(PHP 5, PHP 7, PHP 8)
mysqli_stmt::$num_rows -- mysqli_stmt::num_rows -- mysqli_stmt_num_rows — Returns the number of rows fetched from the server
Description
Object-oriented style
public mysqli_stmt::num_rows(): int|string
This function returns 0
unless all rows have been fetched from the server.
Return Values
An int representing the number of buffered rows. Returns 0
in unbuffered mode unless all rows have been fetched from the server.
Note:
If the number of rows is greater than
[PHP_INT_MAX](reserved.constants.php#constant.php-int-max)
, the number will be returned as a string.
Examples
Example #1 Object-oriented style
`<?php
mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); mysqli=newmysqli("localhost","myuser","mypassword","world");mysqli = new mysqli("localhost", "my_user", "my_password", "world");mysqli=newmysqli("localhost","myuser","mypassword","world");query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20"; stmt=stmt = stmt=mysqli->prepare($query);
$stmt->execute();/* store the result in an internal buffer */ stmt−>storeresult();printf("Numberofrows:stmt->store_result();printf("Number of rows: %d.\n", stmt−>storeresult();printf("Numberofrows:stmt->num_rows);`
Example #2 Procedural style
`<?php
mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); link=mysqliconnect("localhost","myuser","mypassword","world");link = mysqli_connect("localhost", "my_user", "my_password", "world");link=mysqliconnect("localhost","myuser","mypassword","world");query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20"; stmt=mysqliprepare(stmt = mysqli_prepare(stmt=mysqliprepare(link, $query);
mysqli_stmt_execute($stmt);/* store the result in an internal buffer */
mysqli_stmt_store_result($stmt);printf("Number of rows: %d.\n", mysqli_stmt_num_rows($stmt));`
The above examples will output:
See Also
- mysqli_stmt_store_result() - Stores a result set in an internal buffer
- mysqli_stmt_affected_rows() - Returns the total number of rows changed, deleted, inserted, or matched by the last statement executed
- mysqli_prepare() - Prepares an SQL statement for execution
Found A Problem?
18 years ago
`Please be advised, for people who sometimes miss to read this important Manual entry for this function:
If you do not use mysqli_stmt_store_result( ), and immediatley call this function after executing a prepared statement, this function will usually return 0 as it has no way to know how many rows are in the result set as the result set is not saved in memory yet.
mysqli_stmt_store_result( ) saves the result set in memory thus you can immedietly use this function after you both execute the statement AND save the result set.
If you do not save the result set but still want to use this function you have to actually loop through the result set one row at a time using mysqli_stmt_fetch( ) before using this function to determine the number of rows.
A thought though, if you want to determine the number of rows without storing the result set and after looping through it, why not just simply keep an internal counter in your loop every time a row is fetched and save the function call.
In short, this function is only really useful if you save the result set and want to determine the number of rows before looping through it, otherwise you can pretty much recreate its use like I suggested.
`