I'm working on a forum. To pull the actual forum threads I use
$query = <<<SQL
SELECT *
FROM forum_forums
WHERE category = :category
ORDER BY sticky DESC, lastAnswer DESC
LIMIT :limit
OFFSET :offset
SQL;
$resource = $db->db->prepare( $query );
$resource->execute(array (
":category" => $_GET['ident3'],
":limit" => $limit,
":offset" => $offset,
));
The limit and offset are defined variables, but if I remove them from the query it "works" and fetches results, but ruins the pagination portion, was wondering if there's something wrong with the way I'm preparing this query because it works when I have the query directly inside an execute function and use the $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
I just really don't care to run my queries this way.
Above the query we also have.
try {
$query = <<<SQL
SELECT COUNT(*)
FROM forum_forums
WHERE category = :category
SQL;
$resource = $db->db->prepare( $query );
$resource->execute( array (
":category" => $_GET['ident3'],
));
$total = $resource->fetchColumn();
$limit = 1;
$pages = ceil($total / $limit);
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
$offset = ($page - 1) * $limit;