Trying to do the dreaded pagination using PDO, but I can't find an answer fitting my situation:
$pageVar = 10;
$startRowLimit = ($page * $pageVar) - $pageVar; // returns correct value
$articlesQuery = "SELECT
`ID`,`Title`,`Text`,`Poster`,`Date`,
( SELECT `ID` FROM users WHERE article.`Poster` = users.`Username` )
FROM article WHERE `Visible` = 1 ORDER BY `Date` DESC LIMIT ? , ? ";
$articles = $mysqli->selectAll($articlesQuery, array($startRowLimit, $pageVar));
Here's selectAll function:
public function selectAll($query, $params){
$this->sql = $this->dbh->prepare($query);
$this->sql->execute($params);
$result = $this->sql->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
How can I make it work? I tried doing
array((int) $startRowLimit, (int) $pageVar)
But that doesn't work. Any idea?
You don't need to use mark for limit since they doens't come from user input. Just put your int variables in the query
DESC LIMIT {$offset} , {$var}
Anyway the right syntax is:
$sql = "SELECT * [...] LIMIT :start, :results";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':start', $_GET['start'], PDO::PARAM_INT);
$stmt->bindParam(':results', $_GET['results'], PDO::PARAM_INT);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With