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

      2. <i id='IB76h'><tr id='IB76h'><dt id='IB76h'><q id='IB76h'><span id='IB76h'><b id='IB76h'><form id='IB76h'><ins id='IB76h'></ins><ul id='IB76h'></ul><sub id='IB76h'></sub></form><legend id='IB76h'></legend><bdo id='IB76h'><pre id='IB76h'><center id='IB76h'></center></pre></bdo></b><th id='IB76h'></th></span></q></dt></tr></i><div id='IB76h'><tfoot id='IB76h'></tfoot><dl id='IB76h'><fieldset id='IB76h'></fieldset></dl></div>
        <tfoot id='IB76h'></tfoot>
          <bdo id='IB76h'></bdo><ul id='IB76h'></ul>
        <legend id='IB76h'><style id='IB76h'><dir id='IB76h'><q id='IB76h'></q></dir></style></legend>
      3. PHP &amp;mySQL:实现事务的简单代码 - Commit &amp;回滚

        时间:2023-10-10

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

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

                  <tfoot id='nINTG'></tfoot>

                  <i id='nINTG'><tr id='nINTG'><dt id='nINTG'><q id='nINTG'><span id='nINTG'><b id='nINTG'><form id='nINTG'><ins id='nINTG'></ins><ul id='nINTG'></ul><sub id='nINTG'></sub></form><legend id='nINTG'></legend><bdo id='nINTG'><pre id='nINTG'><center id='nINTG'></center></pre></bdo></b><th id='nINTG'></th></span></q></dt></tr></i><div id='nINTG'><tfoot id='nINTG'></tfoot><dl id='nINTG'><fieldset id='nINTG'></fieldset></dl></div>
                  本文介绍了PHP &amp;mySQL:实现事务的简单代码 - Commit &amp;回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的平台:

                  PHP &mysql

                  PHP & mySQL

                  我的情况:

                  我正在尝试在我的代码中实现事务.我试图遵循示例,但没有太大帮助.我正在运行 3 个查询,我想以这样的方式编写一个事务,以便如果任何查询失败,整个事务应该回滚.我真的很感激一个简单、高效且非面向对象的 PHP 代码来实现这个目标.提前致谢.

                  I am trying to implement transactions within my code. I tried to follow examples, but it's not much help. I am running 3 queries and I wanted to write a transaction in such a way so that if any of the query(ies) fail, the whole transaction should roll back. I would really appreciate a simple, efficient and non-object oriented PHP code to achieve this goal. Thank you in advance.

                  我的 PHP 代码:

                  //db_res calls a custom function that performs a mysql_query on the query
                  $res1 = db_res("SELECT c1, c2 FROM t1 WHERE c5 = 3");
                  $res2 = db_res("UPDATE t2 SET c1 = 5 WHERE c2 = 10");
                  $res3 = db_res("DELETE FROM t3 WHERE c1 = 20");
                  
                  if( $res1 && $res2 && $res3 )
                  {
                   //commit --- but how?
                  }
                  else
                  {
                   //rollback --- but how?
                  }
                  

                  推荐答案

                  你需要使用mysqli 扩展以使用此功能.

                  You need to use the mysqli extension to use this functionality.

                  参见:autocommit()、commit() 和 rollback()

                  <?php
                  $link = mysqli_connect("localhost", "my_user", "my_password", "world");
                  
                  /* check connection */
                  if (mysqli_connect_errno()) {
                      printf("Connect failed: %s
                  ", mysqli_connect_error());
                      exit();
                  }
                  
                  /* disable autocommit */
                  mysqli_autocommit($link, FALSE);
                  
                  mysqli_query($link, "CREATE TABLE myCity LIKE City");
                  mysqli_query($link, "ALTER TABLE myCity Type=InnoDB");
                  mysqli_query($link, "INSERT INTO myCity SELECT * FROM City LIMIT 50");
                  
                  /* commit insert */
                  mysqli_commit($link);
                  
                  /* delete all rows */
                  mysqli_query($link, "DELETE FROM myCity");
                  
                  if ($result = mysqli_query($link, "SELECT COUNT(*) FROM myCity")) {
                      $row = mysqli_fetch_row($result);
                      printf("%d rows in table myCity.
                  ", $row[0]);
                      /* Free result */
                      mysqli_free_result($result);
                  }
                  
                  /* Rollback */
                  mysqli_rollback($link);
                  
                  if ($result = mysqli_query($link, "SELECT COUNT(*) FROM myCity")) {
                      $row = mysqli_fetch_row($result);
                      printf("%d rows in table myCity (after rollback).
                  ", $row[0]);
                      /* Free result */
                      mysqli_free_result($result);
                  }
                  
                  /* Drop table myCity */
                  mysqli_query($link, "DROP TABLE myCity");
                  
                  mysqli_close($link);
                  ?>
                  

                  这篇关于PHP &amp;mySQL:实现事务的简单代码 - Commit &amp;回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP-MySQL-如何安全地增加 MySQL 整数字段? 下一篇:如果查询失败,是否需要编写 ROLLBACK?

                  相关文章

                    <bdo id='936lK'></bdo><ul id='936lK'></ul>

                    <legend id='936lK'><style id='936lK'><dir id='936lK'><q id='936lK'></q></dir></style></legend>
                  1. <small id='936lK'></small><noframes id='936lK'>

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