这是我的代码的快照:
$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);
Run Code Online (Sandbox Code Playgroud)
我明白了
您的SQL语法有错误; 检查与MySQL服务器版本对应的手册,以便在第1行的"15",15'附近使用正确的语法
似乎PDO在SQL代码的LIMIT部分中为我的变量添加单引号.我查了一下我发现了这个我认为相关的错误:http: //bugs.php.net/bug.php?id = 44639
这就是我在看的东西吗?这个bug自2008年4月开始!在此期间我们应该做什么?
我需要构建一些分页,并且需要在发送sql语句之前确保数据是干净的,sql注入安全的.
我收到了这个恼人的错误,虽然我知道为什么我得到它,但我不能为我的生活找到解决方案.
if ($limit) {
$sth->bindValue(':page', $page - 1, PDO::PARAM_INT);
$sth->bindValue(':entries_per_page', $page * $entries_per_page, PDO::PARAM_INT);
}
$sth->execute($criteria);
Run Code Online (Sandbox Code Playgroud)
查询包含占位符(:placeholder
).但是要添加那些LIMIT占位符,我需要使用手动方法(bindValue
),否则引擎会将它们变成字符串.
我没有得到无效的参数数量错误,因此所有占位符都已正确绑定(我假设).
查询:
SELECT `articles`.*, `regional_municipalities`.`name` AS `regional_municipality_name`,
`_atc_codes`.`code` AS `atc_code`, `_atc_codes`.`name` AS `substance`
FROM `articles`
LEFT JOIN `_atc_codes`
ON (`_atc_codes`.`id` = `articles`.`atc_code`)
JOIN `regional_municipalities`
ON (`regional_municipalities`.`id` = `articles`.`regional_municipality`)
WHERE TRUE AND `articles`.`strength` = :strength
GROUP BY `articles`.`id`
ORDER BY `articles`.`id`
LIMIT :page, :entries_per_page
Run Code Online (Sandbox Code Playgroud)
所有占位符值都位于$ criteria中,除了我手动绑定的最后两个LIMIT bindValue()
.
我在我的应用程序中使用PDO.但是当我在包含的查询中使用预准备语句时,我遇到了问题LIMIT
.有什么问题?
代码:
$start = 0;
$rows = 20;
$sql = "SELECT * FROM tbl_news ORDER BY date DESC LIMIT ?, ?";
$q = $db->prepare($sql);
$q->execute(array($start , $rows));
Run Code Online (Sandbox Code Playgroud)
错误:
检查与MySQL服务器版本对应的手册,以便在"0","20"附近使用正确的语法
我理解它是如何LIMIT
工作的,但我想知道是否有办法在之前设置数据库查询的起点LIMIT
.这可能吗?我对此有意义吗?