This commit is contained in:
Flatlogic Bot 2026-02-27 03:03:32 +00:00
parent bd73e23131
commit e2cbec0383

View File

@ -72,7 +72,7 @@ function get_product_price($product) {
}
/**
* Paginate a query result.
* Paginate a query result.
*
* @param PDO $pdo The PDO connection object.
* @param string $query The base SQL query (without LIMIT/OFFSET).
@ -107,7 +107,9 @@ function paginate_query($pdo, $query, $params = [], $default_limit = 20) {
}
// Count total rows using a subquery to handle complex queries safely
$count_sql = "SELECT COUNT(*) FROM ($query) as count_table";
// Strip ORDER BY from the query for the count to avoid SQL errors and improve performance
$count_query = preg_replace('/ORDER\s+BY.*$/is', '', $query);
$count_sql = "SELECT COUNT(*) FROM ($count_query) as count_table";
$stmt = $pdo->prepare($count_sql);
$stmt->execute($params);
$total_rows = $stmt->fetchColumn();