理解bindParam和bindValue之間的區別
問題:
問題:根本區別PDOStatement::bindParam()與之間PDOStatement::bindValue()?
答案:根據 PDOStatement::bindParam 手冊條目,關鍵區別在於兩個方法的引用行為。 bindParam 將變數綁定為引用,而bindValue 則綁定變數的值。此參考行為在 PDOStatement::execute() 執行期間發揮作用。
bindParam 範例:$sex = 'male'; $s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex'); $s->bindParam(':sex', $sex); // bind the variable using bindParam $sex = 'female'; $s->execute(); // execute with WHERE sex = 'female'
這裡,bindParam 綁定了 $sex 作為參考。當該語句執行時,它引用 $sex 的當前值,該值已變更為「female」。因此,查詢將檢索「女性」學生的結果。
bindValue 範例:$sex = 'male'; $s = $dbh->prepare('SELECT name FROM students WHERE sex = :sex'); $s->bindValue(':sex', $sex); // bind the variable's value using bindValue $sex = 'female'; $s->execute(); // execute with WHERE sex = 'male'
以上是bindParam 與 bindValue:PHP PDO 的主要差異是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!