sil*_*ire 19 php mysql pdo prepared-statement
我收到了这个恼人的错误,虽然我知道为什么我得到它,但我不能为我的生活找到解决方案.
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()
.
Now*_*een 25
当一个绑定具有相同参数名称的两个值时,可以发出相同的错误2031,如:
$sth->bindValue(':colour', 'blue');
$sth->bindValue(':colour', 'red');
..所以,要小心.
如果您尝试使用占位符运行查询而不是准备诸如的语句,则也会出现此异常
$stmt = $db->query('SELECT * FROM tbl WHERE ID > ?');
Run Code Online (Sandbox Code Playgroud)
代替
$stmt = $db->prepare('SELECT * FROM tbl WHERE ID > ?');
Run Code Online (Sandbox Code Playgroud)