I'm running a fairly complex MySQL query (multiple LEFT JOIN
, GROUP_CONCAT
and bound parameters) with PDO. Due to the complexity of the query I have added a LIMIT
clause to the end to return a particular set of the data - which is shown in a paginated table in my application.
The pagination requires the total number of results to be found. I didn't realise this was so awkward but have read the following:
- get total for limit in mysql using same query?
- Get total number of rows when using LIMIT?
- How to count all rows when using SELECT with LIMIT in MySQL query?
The performance of SQL_CALC_FOUND_ROWS
was terrible so I can't use this.
The only way I could think to write this was to run a second query, using COUNT
. But because the query uses parameter binding (for search terms the user has entered), I have to bind them again in the second query.
My initial query to get the results looks like this:
// Get the results
$sql = ' '; // Complex query
$limit = ' LIMIT ' . $start . ', ' . $offset;
$stmt = $connection->prepare($sql);
// Search parameters
if (isset($search['term1'])) {
$stmt->bindValue('term1', '%' . $search['term1'] . '%');
}
if (isset($search['term2'])) {
$stmt->bindValue('term2', '%' . $search['term2'] . '%');
}
if (isset($search['term3'])) {
$stmt->bindValue('term3', '%' . $search['term3'] . '%');
}
$stmt->execute();
return $stmt->fetchAll('assoc');
Now I need to remove the LIMIT
so I've done the following:
$stmt_count = $connection->prepare('SELECT COUNT(*) FROM (' . $sql . ') AS total_results');
$stmt_count->execute();
But this doesn't work, because I'll need to bind the parameters again. Surely there's a more efficient way, than repeating all of the binding on to $stmt_count
??
if (isset($search['term1'])) {
$stmt_count->bindValue('term1', '%' . $search['term1'] . '%');
}
if (isset($search['term2'])) {
$stmt_count->bindValue('term2', '%' . $search['term2'] . '%');
}
if (isset($search['term3'])) {
$stmt_count->bindValue('term3', '%' . $search['term3'] . '%');
}
Incidentally, I cannot believe how something as basic as this has been made so difficult! It's a very basic requirement to be able to easily know the total number of results of a query.