PDO bindParam与执行 [英] PDO bindParam vs. execute

查看:50
本文介绍了PDO bindParam与执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到将bindParambindValue与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方法而不是将参数传递给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中传递值时,所有值都被视为字符串(即使提供了整数值).因此,如果需要强制使用数据类型,则应始终使用bindValuebindParam.

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与执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆