gpt4 book ai didi

php - 如何在 LIMIT 子句中应用 bindValue 方法?

转载 作者:行者123 更新时间:2023-11-29 20:06:15 24 4
gpt4 key购买 nike

这是我的代码的快照:

$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/40340212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com