本文介紹了PDO:“無效的參數號"用相同的值替換多個參數時的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如果我的參數在查詢中多次出現,我該如何綁定,如下所示?
How do I bind my parameter if it appears multiple times in the query as follows?
$STH = $DBH->prepare("SELECT * FROM $table WHERE firstname LIKE :string OR lastname LIKE :string");
$STH->bindValue(':string', '%'.$string.'%', PDO::PARAM_STR);
$result = $STH->execute();
推薦答案
您提到了準備語句的兩個參數(同名),但您只為第一個參數提供了一個值(這就是錯誤所在).
You mentioned two parameters (with the same name) for the prepare statement, yet you supply a value for the first parameter only (that's what the error was about).
不太清楚 PDO 如何在內部解決相同參數名稱的問題,但您總是可以避免這種情況.
Not quite sure how PDO internally solved the same-parameter-name issue, but you can always avoid that.
兩種可能的解決方案:
$sql = "select * from $table ".
"where "
"first_name like concat('%', :fname, '%') or ".
"last_name like concat('%', :lname, '%')";
$stmt= $DBH->prepare($sql);
$stmt->bindValue(':fname', $string, PDO::PARAM_STR);
$stmt->bindValue(':lname', $string, PDO::PARAM_STR);
<小時>
$sql = "select * from $table ".
"where "
"first_name like concat('%', ?, '%') or ".
"last_name like concat('%', ?, '%')";
$stmt= $DBH->prepare($sql);
$stmt->bindValue(1, $string, PDO::PARAM_STR);
$stmt->bindValue(2, $string, PDO::PARAM_STR);
<小時>
順便說一下,您現有的方式仍然存在 SQL 注入問題.
By the way, the existing way you have done still has SQL injection issues.
這篇關于PDO:“無效的參數號"用相同的值替換多個參數時的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!