||SQL_CALC_FOUND_ROWS - How to know the number of rows a query would have returned if there was no LIMIT set on the query using php5 and mysqli.Usfull with Pagination (Paging).
||PHP, Databases, MySQL
||Jun 08th 2007
3 of 5 (graded 3 times)
No file for this code example.
No Images for this code example.
||More code by Igal Rubinstein
Grade This Code Example
||My Examples List
Many times we use the LIMIT option in MySQL to limit the number of records returned for a given
query. A feature most web developers commonly use is pagination. When showing the little numbers
pointing back and forth to previous and next pages, we need to both know how many records are in
total and at the same time we need to show only one page of results.
Many developers simply run the query twice. Once without the LIMIT to see how many records answer
the query and a second time to actually get the limited data needed to show.
Another approach to this is to use SQL_CALC_FOUND_ROWS. This methos is faster than running the same
query twice. Lets look at a simple query :
//Use $Result to show the data
$Result=mysqli_query($link,"SELECT SQL_CALC_FOUND_ROWS FirstName,LastName From UserBase LIMIT 10");
// The number of rows that actually returned for the query (Can be less than the limit)
//This is the number of results that would have been returned if the query did not have LIMIT 10 at
//the end. You can now use this number to calculate the number of pages in your pagination.
This code example will also work on PHP4, just use mysql_ instead of mysqli_.
From the MySQL Manual :
In the absence of the SQL_CALC_FOUND_ROWS option in the most recent SELECT statement, FOUND_ROWS()
returns the number of rows in the result set returned by that statement.
The row count available through FOUND_ROWS() is transient and not intended to be available past the
statement following the SELECT SQL_CALC_FOUND_ROWS statement. If you need to refer to the value later, save it.
More info at : http://dev.mysql.com/doc/refman/4.1/en/information-functions.html
|bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager|
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
|This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.|
Categories : Databases, MySQL, Complete Programs, PHP, Databases
|Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible|
Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server
|Paginating the mySQL data|
Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP
|Automatically printing the contents of an sql table in MySQL.|
Categories : MySQL, PHP, HTML and PHP, Databases
|for each record, do this to the first record, and do that to any subsequent record|
Categories : PHP, Databases, MySQL, Beginner Guides
|How can i Preload a 'SELECT MULTIPLE'?|
Categories : HTML and PHP, PHP, MySQL, Databases
|Displaying records of database in more than one page (paging)|
Categories : Databases, MySQL, PHP
|Mimic ASP's GetString function with PHP|
Categories : PHP, Databases, MySQL, Strings
|Powerful php/mysql Pagination for up to 6 URL Params|
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
|I`d like to use the mysql_fetch_row function along with a "randomizer"
function that would give me a random result from a mySQL table.|
Categories : General SQL, MySQL, PHP, Databases
|Browse a MySQL database & draw a tree view & load final items into a template page.|
Categories : MySQL, Complete Programs, Algorithms, PHP, Databases
|This class splits the results of the query into multiple pages like what the search engine does.|
Categories : PHP Classes, PHP, MySQL, Databases
|A Simple Script to randomly select a CSS from MySQL|
Categories : PHP, Databases, MySQL, CSS
|This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change|
Categories : PHP, Arrays, Cookies, MySQL, Databases