問(wèn)題描述
我遇到了這個(gè)煩人的錯(cuò)誤,雖然我知道我為什么會(huì)遇到它,但我終生無(wú)法找到解決方案.
I'm getting this annoying error and although I have an idea of why I'm getting it, I can't for the life of me find a solution to it.
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);
查詢包含占位符(:placeholder
).但是要添加那些 LIMIT 占位符,我需要使用手動(dòng)方法 (bindValue
),否則引擎會(huì)將它們轉(zhuǎn)換為字符串.
Query contains placeholders (:placeholder
). But to add those LIMIT placeholders, I need to use the manual method (bindValue
) because otherwise the engine will turn them into strings.
我沒(méi)有收到 Invalid number of parameters 錯(cuò)誤,因此所有占位符都已正確綁定(我假設(shè)).
I'm not getting the Invalid number of parameters error, so all placeholders have been bound correctly (I assume).
查詢:
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
所有占位符值都位于 $criteria 中,除了最后兩個(gè) LIMIT,我用 bindValue()
手動(dòng)綁定.
All placeholder values reside in $criteria, except for the last two LIMIT, which I manually bind with bindValue()
.
推薦答案
你不能使用 ->bind*
and ->execute($params)
.使用或;如果你將參數(shù)傳遞給 execute()
,那些會(huì)讓 PDO 忘記已經(jīng)通過(guò) ->bind*
綁定的參數(shù).
You cannot use ->bind*
and ->execute($params)
. Use either or; if you pass parameters to execute()
, those will make PDO forget the parameters already bound via ->bind*
.
這篇關(guān)于PDO 錯(cuò)誤:SQLSTATE[HY000]:一般錯(cuò)誤:2031的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!