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

      2. PHP 在循环中准备语句和事务

        时间:2023-07-30
        <tfoot id='jK1pf'></tfoot>
        • <legend id='jK1pf'><style id='jK1pf'><dir id='jK1pf'><q id='jK1pf'></q></dir></style></legend>

            <bdo id='jK1pf'></bdo><ul id='jK1pf'></ul>
              <tbody id='jK1pf'></tbody>
            1. <small id='jK1pf'></small><noframes id='jK1pf'>

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

                  本文介绍了PHP 在循环中准备语句和事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  循环代码中的经典事务:

                  The classic transactions in a loop code:

                  $mysqli->query("START TRANSACTION");
                  foreach ($pdata as $key => $value) {
                      $sql    = "INSERT INTO temp (`fund_id`) VALUES (" . $value . ")";
                      $result = $mysqli->query($sql);
                  }
                  $mysqli->query("COMMIT");
                  

                  然后我们更改为准备好的语句:

                  Then we change to prepared statements:

                  $mysqli->autocommit(FALSE);
                  foreach ($pdata as $key => $value) {
                      $sql  = "INSERT INTO temp (`fund_id`) VALUES (?)";
                      $stmt = $mysqli->prepare($sql);
                      $stmt->bind_param('i', $value);
                      $stmt->execute();
                  }
                  $mysqli->commit();
                  

                  问题:

                  1) 这两个代码是否相同?我是否在带有准备好的语句的第二个代码中遗漏了什么?

                  1) Are these two codes identical? Am I missing something in the second code with prepared statements?

                  2) $mysqli->commit()$mysqli->query("COMMIT") 一样吗?

                  2) Is $mysqli->commit() the same as $mysqli->query("COMMIT")?

                  3) 我是否需要为准备好的语句块添加 $mysqli->query("START TRANSACTION"); 或者当我们设置 autocommit(FALSE) 时事务将自动启动)?

                  3) Do I need to add $mysqli->query("START TRANSACTION"); for the prepared statements block or the transaction will automatically start when we set autocommit(FALSE)?

                  推荐答案

                  可以通过将 preparebind_param 语句拉出循环来优化循环.

                  Your loop can be optimized by pulling the prepare and bind_param statements out of the loop.

                  $value = null;
                  $mysqli->autocommit(FALSE);
                  $sql  = "INSERT INTO temp (`fund_id`) VALUES (?)";
                  $stmt = $mysqli->prepare($sql);
                  $stmt->bind_param('i', $value);
                  foreach ($pdata as $value) {
                      $stmt->execute();
                  }
                  $mysqli->commit();
                  

                  您已使用 autocommit(FALSE) 行关闭自动提交,因此不需要使用 START TRANSACTION 语句.

                  You have turned off autocommit with your autocommit(FALSE) line and therefore don't need to use the START TRANSACTION statement.

                  这篇关于PHP 在循环中准备语句和事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么带有 mysqli 的面向对象 PHP 比过程方法更好 下一篇:mysqli_store_result() 与 mysqli_use_result()

                  相关文章

                  最新文章

                  <legend id='wpnAG'><style id='wpnAG'><dir id='wpnAG'><q id='wpnAG'></q></dir></style></legend>

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