本文介紹了PDO:無效的參數號:混合命名和位置參數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我遇到了這個我以前從未見過的警告:
I have come across this warning I've not seen before:
警告:PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: 無效的參數號:混合命名和位置參數...
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters in...
參考下面的PDO查詢(為了方便閱讀,已經簡化了功能):
Referring to the following PDO query (have simplified the function for ease of reading):
$offset = 0;
$limit = 12;
function retrieve_search_posts($searchfield, $offset, $limit){
$where = array();
$words = preg_split('/[s]+/',$searchfield);
array_unshift($words, '');
unset($words[0]);
$where_string = implode(" OR ", array_fill(0,count($words), "`post_title` LIKE ?"));
$query = "
SELECT p.post_id, post_year, post_desc, post_title, post_date, img_file_name, p.cat_id
FROM mjbox_posts p
JOIN mjbox_images i
ON i.post_id = p.post_id
AND i.cat_id = p.cat_id
AND i.img_is_thumb = 1
AND post_active = 1
WHERE $where_string
ORDER BY post_date
LIMIT :offset, :limit
DESC";
$stmt = $dbh->prepare($query);
foreach($words AS $index => $word){
$stmt->bindValue($index, "%".$word."%", PDO::PARAM_STR);
}
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
$searcharray = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $searcharray;
}
函數和 PDO 查詢在查詢中沒有包含偏移量和限制變量的情況下工作正常.那么是什么原因導致了這個警告?
The function and PDO query works fine without the offset and limit variables included in the query. So what might be causing this warning?
謝謝
推薦答案
Change
LIMIT :offset, :limit
到
LIMIT ?, ?
和
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
到:
$stmt->bindValue($index+1, $offset, PDO::PARAM_INT);
$stmt->bindValue($index+2, $limit, PDO::PARAM_INT);
這篇關于PDO:無效的參數號:混合命名和位置參數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!