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

标签 php mysql sql pdo bindvalue

这是我的代码的快照:

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

相关文章:

php - 禁止跨域飞行前选项

javascript - 使用JavaScript适配跨域iFrame的高度

php - 在 PHP 中使用基本函数

mysql - Docker-mysqld : Table 'mysql.plugin' doesn't exist

php - 创建一个高效的分页系统

mysql - 如何设置独立的 SQL Server

php - 荷兰电话号码的正则表达式

mysql - 如何从MySQL数据库中选择最新日期

mysql - 跨多个非唯一列的唯一记录

java - 在 JasperReports (iReport) 中对数据集的连续数字进行分组