withTotalCount()
Enables SQL_CALC_FOUND_ROWS before executing a paginated query via get()
. This allows you to retrieve the total number of matched rows, regardless of any LIMIT, using getTotalCount()
.
Signature:
public function withTotalCount(): self
Example: Paginated Query with Total Count
// Limit to 10 results per page, but get full row count
$db->setPageLimit(10);
$db->withTotalCount();
$users = $db->get('test_users');
echo "Page count: " . count($users) . "\n";
echo "Total users (all pages): " . $db->getTotalCount();
Note: Make sure to call
withTotalCount()
before the get()
method. Otherwise, no total will be tracked.
Internally this method injects
SQL_CALC_FOUND_ROWS
into your SELECT statement. The following SELECT FOUND_ROWS()
is used in getTotalCount()
.
Use Cases
- Paginated listings (users, orders, products, etc.)
- APIs that require total rows alongside current page
- Dashboards with combined LIMIT and total count