作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码的快照:
$fetchPictures = $PDO->prepare("SELECT *
FROM pictures
WHERE album = :albumId
ORDER BY id ASC
LIMIT :skip, :max");
$fetchPictures->bindValue(':albumId', $_GET['albumid'], PDO::PARAM_INT);
if(isset($_GET['skip'])) {
$fetchPictures->bindValue(':skip', trim($_GET['skip']), PDO::PARAM_INT);
} else {
$fetchPictures->bindValue(':skip', 0, PDO::PARAM_INT);
}
$fetchPictures->bindValue(':max', $max, PDO::PARAM_INT);
$fetchPictures->execute() or die(print_r($fetchPictures->errorInfo()));
$pictures = $fetchPictures->fetchAll(PDO::FETCH_ASSOC);
我明白
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''15', 15' at line 1
PDO 似乎在 SQL 代码的 LIMIT 部分中向我的变量添加了单引号。我查了一下,发现了这个我认为相关的错误: http://bugs.php.net/bug.php?id=44639
这就是我所看到的吗?这个bug从2008年4月就已经开放了!在此期间我们应该做什么?
我需要构建一些分页,并且需要在发送 sql 语句之前确保数据干净、sql 注入(inject)安全。
最佳答案
我记得以前也遇到过这个问题。在将值传递给绑定(bind)函数之前将其转换为整数。我认为这可以解决问题。
$fetchPictures->bindValue(':skip', (int) trim($_GET['skip']), PDO::PARAM_INT);
关于php - 如何在 LIMIT 子句中应用 bindValue 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46738566/
我是一名优秀的程序员,十分优秀!