本文介紹了PDO::bindParam 在 foreach 循環中,所有值都設置為相同嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試使用 a 函數讓重復的數據庫調用變得更容易一些,該函數應該(理論上)允許我使用數組綁定參數:
I am trying to make life a little bit easier for repeated database calls with the a function which should (in theory) allow me to bind parameters using an array:
function query($tblName,$queryParams = false){
$this->queryObject = $this->con->prepare($this->queryString);
if($queryParams){
foreach ($queryParams as $key => $value) {
$this->queryObject->bindParam($key, $value);
}
}
$this->queryResult = $this->queryObject->execute();
print_r($this->queryResult);
}
然后我會這樣稱呼它:
$queryParams = array(':userName' => $_POST['username'], ':password' => $_POST['password'], ':salt'=>$userUser->salt, ':userEmail'=>$_POST['email']);
$registrationDb->query('tblUser', $queryParams);
數據庫連接正在工作,正在寫入,但是,所有值都被設置為 $_POST['email]
,我無法弄清楚為什么會這樣正在發生.
The database connection is working, and there is a write being made, however, all the values are being set to $_POST['email]
, and I can't puzzle out why this is happening.
有沒有更好的方法來做到這一點?
Is there a better way to be doing this?
推薦答案
bindParam
通過引用綁定變量,所以你需要 bindValue
代替:
bindParam
binds variable by reference, so you need bindValue
instead:
$this->queryObject->bindValue($key, $value);
這篇關于PDO::bindParam 在 foreach 循環中,所有值都設置為相同嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!