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;回滚

        PHP amp; mySQL: Simple code to implement Transaction - Commit amp; Rollback(PHP amp;mySQL:实现事务的简单代码 - Commit amp;回滚)

              <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;回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                    <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'>

                            <tbody id='936lK'></tbody>
                            <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>