問題描述
我經??吹绞褂?bindParam
或 bindValue
和 PDO 的代碼.簡單地將參數傳遞給 execute
是否會因任何原因而皺眉?
I often see code using bindParam
or bindValue
with PDO. Is simply passing arguments to execute
frowned upon for any reason?
我知道 bindParam
實際上綁定到變量,并且您可以設置與 bind
方法綁定的參數類型,但是如果您只插入字符串怎么辦?
I understand that bindParam
actually binds to the variables and that you can set the type of parameter being bound with both bind
methods, but what if you are only inserting strings?
$query = "SELECT col1 FROM t1 WHERE col2 = :col2 AND col3 = :col3 AND col4 = :col4";
$pdo->bindValue(':col2', 'col2');
$pdo->bindValue(':col3', 'col3');
$pdo->bindValue(':col4', 'col4');
我經常看到以上內容,但我個人更喜歡:
I often see the above, but personally I prefer:
$pdo->execute(array(':col2' => 'col2', ':col3' => 'col3', ':col4' => 'col4'));
它沒有那么冗長,而且從視覺上看,讓輸入一起進入"查詢對我來說更有意義.但是,我幾乎沒有看到它被使用過.
It is not as verbose and visually it makes more sense to me to have the inputs "going in" to the query together. However, I hardly ever see it used.
當您不必利用 bind
方法的特殊行為時,是否有理由更喜歡 bind
方法而不是將參數傳遞給 execute
?>
Is there a reason to prefer the bind
methods over passing parameters to execute
when you don't have to take advantage of the special behaviors of the former?
推薦答案
您可能會發現 bindParam
在您只想將變量引用綁定到查詢中的參數時使用,但也許仍然需要對其進行一些操作,只需要在查詢執行時計算的變量值.它還允許您執行更復雜的操作,例如將參數綁定到存儲過程調用并將返回值更新到綁定變量中.
You might find bindParam
used when you just want to bind a variable reference to a parameter in the query, but perhaps still need to do some manipulations on it and only want the value of the variable calculated at time of query execution. It also allows you to do more complex things like bind a parameter to a stored procedure call and have the returned value updated into the bound variable.
有關更多信息,請參閱bindParam 文檔,bindValue 文檔 和 執行文檔.
For more, see the bindParam documentation, bindValue documentation and execute documentation.
例如
$col1 = 'some_value';
$pdo->bindParam(':col1', $col1);
$col1 = 'some_other_value';
$pdo->execute(); // would use 'some_other_value' for ':col1' parameter
bindValue
和將數組傳遞給 execute
的行為與參數值在該點固定并相應地執行 SQL 的方式大致相同.
bindValue
and passing an array to execute
behave in much the same way as the parameter value is fixed at that point and SQL executed accordingly.
遵循上面相同的示例,但使用 bindValue
Following the same example above, but using bindValue
$col1 = 'some_value';
$pdo->bindValue(':col1', $col1);
$col1 = 'some_other_value';
$pdo->execute(); // would use 'some_value' for ':col1' parameter
當直接在 execute
中傳遞值時,所有值都被視為字符串(即使提供了整數值).因此,如果您需要強制執行數據類型,則應始終使用 bindValue
或 bindParam
.
When passing values directly in execute
all values are treated as strings (even if integer value is provided). So if you need to enforce data types, you should always use bindValue
or bindParam
.
我認為您可能會看到 bind*
比 execute(array)
使用得更多,因為許多人認為在參數聲明中顯式定義數據類型是更好的編碼實踐.
I think you might see bind*
used more than execute(array)
as many consider it to be better coding practice to explicitly define data types in parameter declarations.
這篇關于PDO bindParam 與執行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!