• <small id='uE8xf'></small><noframes id='uE8xf'>

  • <tfoot id='uE8xf'></tfoot>

      <bdo id='uE8xf'></bdo><ul id='uE8xf'></ul>

      1. <i id='uE8xf'><tr id='uE8xf'><dt id='uE8xf'><q id='uE8xf'><span id='uE8xf'><b id='uE8xf'><form id='uE8xf'><ins id='uE8xf'></ins><ul id='uE8xf'></ul><sub id='uE8xf'></sub></form><legend id='uE8xf'></legend><bdo id='uE8xf'><pre id='uE8xf'><center id='uE8xf'></center></pre></bdo></b><th id='uE8xf'></th></span></q></dt></tr></i><div id='uE8xf'><tfoot id='uE8xf'></tfoot><dl id='uE8xf'><fieldset id='uE8xf'></fieldset></dl></div>
        <legend id='uE8xf'><style id='uE8xf'><dir id='uE8xf'><q id='uE8xf'></q></dir></style></legend>

        PDO bindParam 与执行

        时间:2023-09-21
      2. <tfoot id='FuDLb'></tfoot>

            <bdo id='FuDLb'></bdo><ul id='FuDLb'></ul>

              1. <small id='FuDLb'></small><noframes id='FuDLb'>

                  <tbody id='FuDLb'></tbody>
                  <i id='FuDLb'><tr id='FuDLb'><dt id='FuDLb'><q id='FuDLb'><span id='FuDLb'><b id='FuDLb'><form id='FuDLb'><ins id='FuDLb'></ins><ul id='FuDLb'></ul><sub id='FuDLb'></sub></form><legend id='FuDLb'></legend><bdo id='FuDLb'><pre id='FuDLb'><center id='FuDLb'></center></pre></bdo></b><th id='FuDLb'></th></span></q></dt></tr></i><div id='FuDLb'><tfoot id='FuDLb'></tfoot><dl id='FuDLb'><fieldset id='FuDLb'></fieldset></dl></div>
                • <legend id='FuDLb'><style id='FuDLb'><dir id='FuDLb'><q id='FuDLb'></q></dir></style></legend>

                  本文介绍了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 方法的特殊行为时,是否有理由更喜欢 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 与执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么 PDO 比 mysql_real_escape_string 更适合转义 My 下一篇:在布尔值上调用成员函数 fetch()

                  相关文章

                  最新文章

                  <tfoot id='3Fxue'></tfoot>
                  1. <small id='3Fxue'></small><noframes id='3Fxue'>

                    1. <legend id='3Fxue'><style id='3Fxue'><dir id='3Fxue'><q id='3Fxue'></q></dir></style></legend>
                      <i id='3Fxue'><tr id='3Fxue'><dt id='3Fxue'><q id='3Fxue'><span id='3Fxue'><b id='3Fxue'><form id='3Fxue'><ins id='3Fxue'></ins><ul id='3Fxue'></ul><sub id='3Fxue'></sub></form><legend id='3Fxue'></legend><bdo id='3Fxue'><pre id='3Fxue'><center id='3Fxue'></center></pre></bdo></b><th id='3Fxue'></th></span></q></dt></tr></i><div id='3Fxue'><tfoot id='3Fxue'></tfoot><dl id='3Fxue'><fieldset id='3Fxue'></fieldset></dl></div>
                      • <bdo id='3Fxue'></bdo><ul id='3Fxue'></ul>