Php select count rows
- How to pass PHP Variables by reference ?
- How to format Phone Numbers in PHP ?
- How to use php serialize() and unserialize() Function
- Implementing callback in PHP
- PHP | Merging two or more arrays using array_merge()
- PHP program to print an arithmetic progression series using inbuilt functions
- How to prevent SQL Injection in PHP ?
- How to extract the user name from the email ID using PHP ?
- How to count rows in MySQL table in PHP ?
- How to parse a CSV File in PHP ?
- How to generate simple random password from a given string using PHP ?
- How to upload images in MySQL using PHP PDO ?
- How to check foreach Loop Key Value in PHP ?
- How to properly Format a Number With Leading Zeros in PHP ?
- How to get a File Extension in PHP ?
PHP Date Based
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
PHP String Based
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?
PHP Class Based
PHP JSON Based
PHP File Systems Based
mysqli_num_rows
The behaviour of mysqli_num_rows() depends on whether buffered or unbuffered result sets are being used. This function returns 0 for unbuffered result sets unless all rows have been fetched from the server.
Parameters
Return Values
An int representing the number of fetched 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 , the number will be returned as a string .
Examples
Example #1 Object-oriented style
mysqli_report ( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
$mysqli = new mysqli ( «localhost» , «my_user» , «my_password» , «world» );
$result = $mysqli -> query ( «SELECT Code, Name FROM Country ORDER BY Name» );
/* Get the number of rows in the result set */
$row_cnt = $result -> num_rows ;
printf ( «Result set has %d rows.\n» , $row_cnt );
Example #2 Procedural style
mysqli_report ( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
$link = mysqli_connect ( «localhost» , «my_user» , «my_password» , «world» );
$result = mysqli_query ( $link , «SELECT Code, Name FROM Country ORDER BY Name» );
/* Get the number of rows in the result set */
$row_cnt = mysqli_num_rows ( $result );
printf ( «Result set has %d rows.\n» , $row_cnt );
The above examples will output:
Notes
Note:
In contrast to the mysqli_stmt_num_rows() function, this function doesn’t have object-oriented method variant. In the object-oriented style, use the getter property.
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
- mysqli_stmt_num_rows() — Returns the number of rows fetched from the server
User Contributed Notes 3 notes
If you have problems making work this num_rows, you have to declare ->store_result() first.
$mysqli = new mysqli ( «localhost» , «root» , «» , «tables» );
$query = $mysqli -> prepare ( «SELECT * FROM table1» );
$query -> execute ();
$query -> store_result ();
This function doesn’t work with LIMIT used jointly with SQL_CALC_FOUND_ROWS. If you want to obtain the total rows found you must do it manually, example:
public function errorList ( int $limit = 25 , int $offset = 0 ) $errorList = array();
$result = $this -> con -> query ( «SELECT SQL_CALC_FOUND_ROWS id, erreur FROM Erreurs ORDER BY id DESC LIMIT $limit OFFSET $offset » );
while( $row = $result -> fetch_assoc ()) $errorList [] = new Erreur ( $row );
>
$result -> free ();
// $foundRows = $result->num_rows; // 25
$foundRows = $this -> con -> query ( «SELECT FOUND_ROWS() as foundRows» );
$this -> foundRows = $foundRows -> fetch_assoc (); // 178
return $errorList ;
>
?>
in php 5.3.8 had unexpected troubles when checking for mysqli_result::$num_rows
If the result of the query is empty then var_dump of the result will be like this:
class mysqli_result#5 (5) public $current_field => NULL
public $field_count => NULL
public $lengths => NULL
public $num_rows => NULL
public $type => NULL
>
but var_dump($result->num_rows) will give integer-typed zero instead of NULL:
int(0)
Select Count Function From a MySQL Table in PHP
- Count Rows in a MySQL Table With the Select Count(*) Function in PHP
- Display the Total Records Returned by a Query in a MySQL Table With the Select Count(*) Function in PHP
This tutorial will introduce the select count(*) function, count rows, and get the total records returned by a query from a MySQL table in PHP. Together with the tutorial are working sample codes and their output.
The first step is connecting to the MySQL table database in PHP. Below is a sample code we use to connect to our database.
php $user = 'root'; $pass = ''; $db = 'sample tutorial'; //Replace 'sample tutorial' with the name of your database $con = mysqli_connect("localhost", $user, $pass, $db); //The code does not have an output, so we decided to print 'Database Connected' echo "Database Connected"; ?>
Remember to replace sample tutorial with the name of the database containing your MySQL table.
The above code usually displays no output when successfully connected. We decided to print Database Connected as the optional output in our case.
The table in our database is as follows: