Posts: 366
Name: Steve
Location: Miami, FL, Earth
|
The proper way to do this is with 2 queries:
PHP Code:
$page = 1; $perPage = 10; $start = ($page - 1) * $perPage + 1; $where = " WHERE condition = 'value'";
$countSql = "SELECT COUNT(1) as num_records FROM my_table {$where}"; $resultSql = "SELECT * FROM my_table {$where} LIMIT {$start}, {$perPage}";
$count = mysql_query($countSql); $count = mysql_fetch_assoc($count); $total = $count["num_records"]; // Total number of records in your table
$results = mysql_query($resultSql);
$displayCount = mysql_num_rows($results); // $end = $start + $displayCount;
$displayText = "Displaying {$start} to {$end} of {$total} results.";
The first query will return the total number of records matching your WHERE clause, and the second will return 10 records on the page specified.
If you use only the 2nd query without a limit, you'll retrieve all the data from your database (could be thousands or even millions of records), which will put an unnecessary load on the DB.
The above is the proper solution for maximum performance.
__________________
- Steve
President, Please login or register to view this content. Registration is FREE
Last edited by smoseley; 10-01-2010 at 10:04 AM..
|