尝试使用PDO进行可怕的分页,但我找不到符合我情况的答案:
$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));
这是selectAll函数:
public function selectAll($query, $params){
$this->sql = $this->dbh->prepare($query);
$this->sql->execute($params);
$result = $this->sql->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
我怎样才能让它发挥作用?我试着做了
array((int) $startRowLimit, (int) $pageVar)
但这不起作用。有什么想法吗?
答案 0 :(得分:6)
您不需要使用标记作为限制,因为它们不是来自用户输入。只需将int变量放入查询
即可DESC LIMIT {$offset} , {$var}
无论如何,正确的语法是:
$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);