php PDO bindParam 与执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12392424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO bindParam vs. execute
提问by Explosion Pills
I often see code using bindParamor bindValuewith PDO. Is simply passing arguments to executefrowned upon for any reason?
我经常看到使用PDObindParam或bindValue与 PDO 一起使用的代码。只是将参数传递给execute不赞成的任何理由吗?
I understand that bindParamactually binds to the variables and that you can set the type of parameter being bound with both bindmethods, but what if you are only inserting strings?
我知道bindParam实际上绑定到变量,并且您可以设置与这两种bind方法绑定的参数类型,但是如果您只插入字符串怎么办?
$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.
它并不那么冗长,而且从视觉上看,让输入一起“进入”查询对我来说更有意义。但是,我几乎从未见过它被使用过。
Is there a reason to prefer the bindmethods over passing parameters to executewhen you don't have to take advantage of the special behaviors of the former?
当您不必利用前者的特殊行为时,是否有理由更喜欢这些bind方法而不是传递参数execute?
回答by Mike Brant
You might find bindParamused 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当您只想将变量引用绑定到查询中的参数时,您可能会发现使用,但可能仍然需要对其进行一些操作,并且只想在查询执行时计算变量的值。它还允许您执行更复杂的操作,例如将参数绑定到存储过程调用并将返回值更新到绑定变量中。
For more, see the bindParam documentation, bindValue documentationand execute documentation.
有关更多信息,请参阅bindParam 文档、bindValue 文档和执行文档。
For example
例如
$col1 = 'some_value';
$pdo->bindParam(':col1', $col1);
$col1 = 'some_other_value';
$pdo->execute(); // would use 'some_other_value' for ':col1' parameter
bindValueand passing an array to executebehave in much the same way as the parameter value is fixed at that point and SQL executed accordingly.
bindValue并传递一个数组,其execute行为与参数值在该点固定并相应地执行 SQL 的方式大致相同。
Following the same example above, but using bindValue
按照上面的相同示例,但使用 bindValue
$col1 = 'some_value';
$pdo->bindValue(':col1', $col1);
$col1 = 'some_other_value';
$pdo->execute(); // would use 'some_value' for ':col1' parameter
When passing values directly in executeall values are treated as strings (even if integer value is provided). So if you need to enforce data types, you should always use bindValueor bindParam.
当直接在execute所有值中传递值时,所有值都被视为字符串(即使提供了整数值)。因此,如果您需要强制执行数据类型,则应始终使用bindValue或bindParam。
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.
我认为您可能会看到bind*使用更多,execute(array)因为许多人认为在参数声明中显式定义数据类型是更好的编码实践。
回答by Jens Kooij
By passing the parameters along with the $pdo->execute()method, all values in the array with be passed, as PDO::PARAM_STRto the statement with the $pdo->bindParam()function.
通过将参数与$pdo->execute()方法一起传递,数组中的所有值都将被传递,PDO::PARAM_STR对于带有$pdo->bindParam()函数的语句。
The main difference that I can see now, is that with the $pdo->bindParam()function, you can define the data type passed along, using the PDO::PARAM_*constants as described in the PHP.net manual
我现在可以看到的主要区别是,使用该$pdo->bindParam()函数,您可以使用PHP.net 手册中PDO::PARAM_*描述的常量定义传递的数据类型
回答by Teerath Kumar
Simple, the value of bindParam may change but the value of bindValue can not change. Example:
很简单,bindParam 的值可以改变但bindValue 的值不能改变。例子:
$someVal=10;
$someVal2=20;
/* In bindParam, the value argument is not bound and
will be changed if we change its value before execute.
*/
$ref->bindParam(':someCol',$someVal);
$someVal=$someVal2;
$ref->execute();
//someCol=20
/* In bindValue, the value argument is bound and
never changed if we change its value before execute.
*/
$ref->bindValue(':someCol',$someVal);
// here assignment is referral (&$someVal)
$someVal=$someVal2;
$ref->execute();
//someCol=10

