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

  2. <legend id='rHrs7'><style id='rHrs7'><dir id='rHrs7'><q id='rHrs7'></q></dir></style></legend>

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

      如果在循环中使用 MySQLi 准备好的语句,我什么时

      时间:2023-07-29

            <bdo id='7kqbz'></bdo><ul id='7kqbz'></ul>
          • <small id='7kqbz'></small><noframes id='7kqbz'>

              <tbody id='7kqbz'></tbody>

          • <legend id='7kqbz'><style id='7kqbz'><dir id='7kqbz'><q id='7kqbz'></q></dir></style></legend>

            • <i id='7kqbz'><tr id='7kqbz'><dt id='7kqbz'><q id='7kqbz'><span id='7kqbz'><b id='7kqbz'><form id='7kqbz'><ins id='7kqbz'></ins><ul id='7kqbz'></ul><sub id='7kqbz'></sub></form><legend id='7kqbz'></legend><bdo id='7kqbz'><pre id='7kqbz'><center id='7kqbz'></center></pre></bdo></b><th id='7kqbz'></th></span></q></dt></tr></i><div id='7kqbz'><tfoot id='7kqbz'></tfoot><dl id='7kqbz'><fieldset id='7kqbz'></fieldset></dl></div>
              <tfoot id='7kqbz'></tfoot>
              • 本文介绍了如果在循环中使用 MySQLi 准备好的语句,我什么时候调用 bind_param ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试学习如何在 MySQLi 中使用准备好的语句来插入数据.

                I am trying to learn how to use prepared statements with MySQLi to insert data.

                尽管准备好的语句因其高效重复执行类似语句的能力而受到称赞,但我似乎无法找到使用 MySQLi 在循环中执行多个语句的示例.我特别对以下内容感到困惑:

                Even though prepared statements are lauded for their ability to efficiently execute similar statements repeatedly, I can't seem to find examples of executing multiple statements in a loop using MySQLi. I'm especially confused about the following:

                • 是在我的循环之前还是在我的循环内调用bind_param
                • 是在调用 bind_param 之前还是之后为我的变量赋值
                • whether to call bind_param before my loop or inside my loop
                • whether to assign values to my variables before or after the call to bind_param

                大多数关于预处理语句的教程都使用 PDO.使用 PDO,可以将一组参数值传递给 execute,从而无需调用 bindParam.MySQLi 不是这种情况.

                Most tutorials on prepared statements use PDO. With PDO, an array of parameter values can be passed to execute, eliminating the need to call bindParam. This is not the case with MySQLi.

                PHP 手册 mysqli_prepare 条目有一个示例,显示了以下操作顺序:

                The PHP manual mysqli_prepare entry has an example that shows the following order of operations:

                1. 为变量赋值
                2. 准备声明
                3. 绑定变量
                4. 执行
                5. 关闭

                根据上述判断,我假设我需要在循环中调用 bind 语句.

                Judging by the above, I had assumed that I would need to call the bind statement within my loop.

                然而,PHP 手册 mysqli_stmt_execute 条目有一个显示以下操作顺序的示例:

                However, the PHP manual mysqli_stmt_execute entry has an example that shows the following order of operations:

                1. 准备声明
                2. 绑定变量
                3. 为变量赋值
                4. 执行
                5. 为变量赋值
                6. 执行
                7. 关闭

                请注意,此示例未显示的一件事是变量首次声明的位置.我认为将未声明的变量传递给 bind_param 会生成一个通知.我终于发现我可以传递未定义的变量,因为它们是通过引用传递的.

                Note that one thing this example doesn't show is where the variables are first declared. I thought passing undeclared variables to bind_param would generate a notice. I finally figured out that I can pass undefined variables because they are passed by reference.

                问题:
                一种操作顺序是否优于另一种?还是仅仅取决于你在做什么?两者之间有什么问题吗?

                Question:
                Is one order of operations preferred over the other? Or does it just depend on what you are doing? Are there any gotchas with one or the other?

                注意:代码已经使用 MySQLi,我不想切换到 PDO(现在切换不在本项目范围内).

                Note: The code already uses MySQLi and I don't want to switch to PDO (switching now is outside the scope of this project).

                推荐答案

                因为参数是通过引用传递的,所以可以在之前给变量赋值> 在调用 bind_param 之后.无论哪种方式都行.

                Because the parameters are passed by reference, you can assign the values to the variables before or after the call to bind_param. Either way will work.

                如果变量是通过传递的,则每次更改它们的值时都需要绑定它们.但是因为它们是通过引用传递的,所以只需要绑定一次即可.

                If the variables were passed by value, you would need to bind them each time you changed their value. But since they are passed by reference, you only need to bind them once.

                在单次执行的情况下,操作顺序并不重要,可能取决于值的来源.在循环的情况下,你一定要在循环之前调用bind_param.

                In the case of a single execute, the order of operation doesn't really matter and may depend on where the values are coming from. In the case of a loop, you should definitely call bind_param before the loop.

                这篇关于如果在循环中使用 MySQLi 准备好的语句,我什么时候调用 bind_param ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 MySQLi 上使用 bind_param() 时是否需要转义数据以 下一篇:什么更有效,为什么:每页一个数据库连接或每个

                相关文章

                最新文章

                • <bdo id='OIcm6'></bdo><ul id='OIcm6'></ul>
              • <legend id='OIcm6'><style id='OIcm6'><dir id='OIcm6'><q id='OIcm6'></q></dir></style></legend>

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

                <small id='OIcm6'></small><noframes id='OIcm6'>

                  <tfoot id='OIcm6'></tfoot>