|
|
|
| Title : |
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). |
| Categories : |
PHP, Databases, MySQL |
 Igal Rubinstein |
| Date : |
Jun 08th 2007 |
| Grade : |
3 of 5 (graded 3 times) |
| Viewed : |
8984 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Igal Rubinstein |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
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 :
|
<?php
//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)
$NumberOfResults=mysqli_num_rows($Result);
$NORResult=mysqli_query($link,"Select FOUND_ROWS()");
$NORRow=mysqli_fetch_array($NORResult);
//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.
$NOR=$NORRow["FOUND_ROWS()"];
?> | |
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 | | | PHP-CSL PHP Code Snippet Library, A very handy application designed to save you many hours by storing all your code snippets, classes and functions. Categories : PHP, Utilities, MySQL, Databases, To PHP | | | phpMyAdmin is intended to handle the adminstration of MySQL
over the web. Categories : Databases, MySQL, Complete Programs, PHP | | | Simple database class Categories : PHP, PHP Classes, MySQL, Databases | | | AJAX Application Categories : PHP, AJAX, Databases, MySQL | | | Link Manager for Link Exchangers Categories : PHP, PHP Classes, Databases, MySQL, CURL | | | This program will take data from a user via a web based form, validate it, show it
to the user for re-validation, and finally insert it into the database. Plenty of
sanity checking on the fields in the form.
Categories : MySQL, HTML and PHP, PHP, Complete Programs, Databases | | | formating MySQL Timestamp to get a propper Output.
related : Timestamp, ereg_replace, mysql, substr Categories : MySQL, PHP, Date Time, Databases | | | Shopping Cart e-Commerce Solution Categories : Complete Programs, PHP, MySQL, Databases | | | Education Center is a set of PHP-scripts to administer a corporate education and examination system via Internet/intranet written in PHP for MySQL.
Categories : PHP, Databases, MySQL, Complete Programs | | | Authorize Me! An authentication script. Categories : MySQL, Databases, Authentication, PHP | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Simple pipe delimited file export program that downloads to a local machine Categories : PHP, Filesystem, Databases, MySQL, HTTP | | | mysql_escape_string Categories : PHP, MySQL, Databases, Strings | |
|
|