【问题标题】:PHP + MySQL transactions examplesPHP + MySQL 事务示例
【发布时间】:2011-02-12 02:06:57
【问题描述】:

我真的没有找到使用 MySQL 事务的 PHP 文件的正常示例。你能给我看一个简单的例子吗?

还有一个问题。我已经做了很多编程并且没有使用事务。我可以在header.php 中放置一个PHP 函数或其他东西,如果一个mysql_query 失败,那么其他的也会失败吗?


我想我已经想通了,对吗?:

mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");

$a1 = mysql_query("INSERT INTO rarara (l_id) VALUES('1')");
$a2 = mysql_query("INSERT INTO rarara (l_id) VALUES('2')");

if ($a1 and $a2) {
    mysql_query("COMMIT");
} else {        
    mysql_query("ROLLBACK");
}

【问题讨论】:

  • 你可以用mysql_query("BEGIN");代替序列mysql_query("SET AUTOCOMMIT=0");mysql_query("START TRANSACTION");
  • Please, don't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDOMySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial.
  • 是否“mysql_query("SET AUTOCOMMIT=0");"将所有连接设置为等待提交功能还是仅用于其相关连接?
  • @Neal,实际上mysql wun die 尽管已被弃用,但它将永远在 PECL 中可用。
  • @Pacerier 被弃用的东西不会“死”。它们是为遗留软件正式举行的,但不再被维护,也不再受到新软件的任何推荐实践的影响。事实依然如此,不要使用mysql

标签: php mysql transactions


【解决方案1】:

我在处理事务时通常使用的想法是这样的(半伪代码)

try {
    // First of all, let's begin a transaction
    $db->beginTransaction();
    
    // A set of queries; if one fails, an exception should be thrown
    $db->query('first query');
    $db->query('second query');
    $db->query('third query');
    
    // If we arrive here, it means that no exception was thrown
    // i.e. no query has failed, and we can commit the transaction
    $db->commit();
} catch (\Throwable $e) {
    // An exception has been thrown
    // We must rollback the transaction
    $db->rollback();
    throw $e; // but the error must be handled anyway
}

请注意,根据这个想法,如果查询失败,则必须抛出异常:
  • PDO 可以做到这一点,具体取决于您的配置方式
  • 否则,使用其他一些 API,您可能必须测试用于执行查询的函数的结果,然后自己抛出异常。

不幸的是,没有魔法。您不能只是在某处放置指令并自动完成事务:您仍然必须指定必须在事务中执行哪组查询。

例如,您经常会在事务之前有几个查询(在begin 之前),在事务之后还有另外几个查询(在commit 之后)或rollback),无论交易中发生了什么(或没有),您都希望执行这些查询。

【讨论】:

  • 如果您正在执行可能引发除 db 异常之外的异常的操作,请务必小心。如果是这样,来自非 db 语句的异常可能会无意中导致回滚(即使所有 db 调用都成功)。通常,即使错误不在数据库端,您也会认为回滚是一个好主意,但有时第 3 方/非关键代码可能会导致不太重要的异常,您仍然想继续交易。
  • 这里的$db 类型是什么? mysqli?
  • @Jake 查看我的答案以获取使用 mysqli 的示例(与 Pascal 的方法类似)。
  • 它可以很容易地修改以捕获PDOException,甚至在需要时检查异常值。 us2.php.net/PDOException
  • $db 是 PDO 对象(连接)。参考:php.net/manual/en/pdo.connections.php
【解决方案2】:

使用 PDO 连接时:

$pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8', $user, $pass, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // this is important
]);

我经常使用以下代码进行事务管理:

function transaction(Closure $callback)
{
    global $pdo; // let's assume our PDO connection is in a global var

    // start the transaction outside of the try block, because
    // you don't want to rollback a transaction that failed to start
    $pdo->beginTransaction(); 
    try
    {
        $callback();
        $pdo->commit(); 
    }
    catch (Exception $e) // it's better to replace this with Throwable on PHP 7+
    {
        $pdo->rollBack();
        throw $e; // we still have to complain about the exception
    }
}

使用示例:

transaction(function()
{
    global $pdo;

    $pdo->query('first query');
    $pdo->query('second query');
    $pdo->query('third query');
});

这样,事务管理代码就不会在整个项目中重复。这是一件好事,因为从这个线程中其他与 PDO 相关的答案来看,很容易在其中犯错。最常见的是忘记重新抛出异常并在 try 块内启动事务。

【讨论】:

    【解决方案3】:

    请检查您使用的是哪个存储引擎。如果是 MyISAM,则不支持Transaction('COMMIT','ROLLBACK'),因为只有 InnoDB 存储引擎,而不是 MyISAM,支持事务。

    【讨论】:

      【解决方案4】:

      另一个带有mysqli_multi_query 的程序样式示例,假设$query 用分号分隔的语句填充。

      mysqli_begin_transaction ($link);
      
      for (mysqli_multi_query ($link, $query);
          mysqli_more_results ($link);
          mysqli_next_result ($link) );
      
      ! mysqli_errno ($link) ?
          mysqli_commit ($link) : mysqli_rollback ($link);
      

      【讨论】:

      • 有点奇怪的代码,但至少它建议使用mysqli_multi_query。任何对此感兴趣的人都应该在其他地方搜索更简洁的示例。
      • 如果查询中有任何用户提供的数据,则不建议使用 mysqli_multi_query()。此函数不支持预处理语句。
      【解决方案5】:
      <?php
      
      // trans.php
      function begin(){
          mysql_query("BEGIN");
      }
      
      function commit(){
          mysql_query("COMMIT");
      }
      
      function rollback(){
          mysql_query("ROLLBACK");
      }
      
      mysql_connect("localhost","Dude1", "SuperSecret") or die(mysql_error());
      
      mysql_select_db("bedrock") or die(mysql_error());
      
      $query = "INSERT INTO employee (ssn,name,phone) values ('123-45-6789','Matt','1-800-555-1212')";
      
      begin(); // transaction begins
      
      $result = mysql_query($query);
      
      if(!$result){
          rollback(); // transaction rolls back
          echo "transaction rolled back";
          exit;
      }else{
          commit(); // transaction is committed
          echo "Database transaction was successful";
      }
      
      ?>
      

      【讨论】:

      • 对于这样一个广泛而引人注目的问题,如果答案也能反映这一点,那就太好了。您的代码示例很棒,但您能详细说明一下吗?解释交易的原因、时间和地点?最后,将代码与您的解释相关联。
      • 欢迎使用 StackOverflow。请始终为您的答案写一些描述性文字。
      • 对不起,我是初学者,我的英语不好,它非常简单的代码示例 - 对于初学者 - commit() rollback() begin() 放在类 DB 中(例如),$query - 不是一次- 也许 $query0 $query1 - 然后检查它们 - 我使用这段代码,这很容易理解 =)
      • 他的 cmets 使示例非常清晰。好的代码不应该需要描述文本。这个问题还要求一个简单的例子。我喜欢这个答案。
      • @GedzbergAlex 对于单个查询不需要事务,只是对事务感到困惑,是否有理由将事务用于单个查询?
      【解决方案6】:

      我做了一个函数来获取查询向量并进行交易,也许有人会发现它有用:

      function transaction ($con, $Q){
              mysqli_query($con, "START TRANSACTION");
      
              for ($i = 0; $i < count ($Q); $i++){
                  if (!mysqli_query ($con, $Q[$i])){
                      echo 'Error! Info: <' . mysqli_error ($con) . '> Query: <' . $Q[$i] . '>';
                      break;
                  }   
              }
      
              if ($i == count ($Q)){
                  mysqli_query($con, "COMMIT");
                  return 1;
              }
              else {
                  mysqli_query($con, "ROLLBACK");
                  return 0;
              }
          }
      

      【讨论】:

        【解决方案7】:

        我想我已经想通了,对吗?:

        mysql_query("START TRANSACTION");
        
        $a1 = mysql_query("INSERT INTO rarara (l_id) VALUES('1')");
        $a2 = mysql_query("INSERT INTO rarara (l_id) VALUES('2')");
        
        if ($a1 and $a2) {
            mysql_query("COMMIT");
        } else {        
            mysql_query("ROLLBACK");
        }
        

        【讨论】:

        • 引擎存储应该是 InnoDB,而不是 MyISAM!
        【解决方案8】:

        我有这个,但不确定这是否正确。这个也可以试试。

        mysql_query("START TRANSACTION");
        $flag = true;
        $query = "INSERT INTO testing (myid) VALUES ('test')";
        
        $query2 = "INSERT INTO testing2 (myid2) VALUES ('test2')";
        
        $result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
        if (!$result) {
        $flag = false;
        }
        
        $result = mysql_query($query2) or trigger_error(mysql_error(), E_USER_ERROR);
        if (!$result) {
        $flag = false;
        }
        
        if ($flag) {
        mysql_query("COMMIT");
        } else {        
        mysql_query("ROLLBACK");
        }
        

        来自这里的想法:http://www.phpknowhow.com/mysql/transactions/

        【讨论】:

        • 代码不正确。 trigger_error 将返回 true(除非你搞砸了你的调用),所以 $result 将始终为 true,因此此代码将错过任何失败的查询,并始终尝试提交。同样令人不安的是,您使用的是旧的已弃用的mysql_query,而不是使用mysqli,即使您链接到使用mysqli 的教程。恕我直言,您应该删除这个不好的示例,或者将其替换为使用 phpknowhow 教程中编写的代码。
        【解决方案9】:

        由于这是 google 上“php mysql transaction”的第一个结果,我想我会添加一个答案,明确演示如何使用 mysqli 执行此操作(如原作者想要的示例)。这是使用 PHP/mysqli 进行事务的简化示例:

        // let's pretend that a user wants to create a new "group". we will do so
        // while at the same time creating a "membership" for the group which
        // consists solely of the user themselves (at first). accordingly, the group
        // and membership records should be created together, or not at all.
        // this sounds like a job for: TRANSACTIONS! (*cue music*)
        
        $group_name = "The Thursday Thumpers";
        $member_name = "EleventyOne";
        $conn = new mysqli($db_host,$db_user,$db_passwd,$db_name); // error-check this
        
        // note: this is meant for InnoDB tables. won't work with MyISAM tables.
        
        try {
        
            $conn->autocommit(FALSE); // i.e., start transaction
        
            // assume that the TABLE groups has an auto_increment id field
            $query = "INSERT INTO groups (name) ";
            $query .= "VALUES ('$group_name')";
            $result = $conn->query($query);
            if ( !$result ) {
                $result->free();
                throw new Exception($conn->error);
            }
        
            $group_id = $conn->insert_id; // last auto_inc id from *this* connection
        
            $query = "INSERT INTO group_membership (group_id,name) ";
            $query .= "VALUES ('$group_id','$member_name')";
            $result = $conn->query($query);
            if ( !$result ) {
                $result->free();
                throw new Exception($conn->error);
            }
        
            // our SQL queries have been successful. commit them
            // and go back to non-transaction mode.
        
            $conn->commit();
            $conn->autocommit(TRUE); // i.e., end transaction
        }
        catch ( Exception $e ) {
        
            // before rolling back the transaction, you'd want
            // to make sure that the exception was db-related
            $conn->rollback(); 
            $conn->autocommit(TRUE); // i.e., end transaction   
        }
        

        另外,请记住 PHP 5.5 有一个新方法 mysqli::begin_transaction。但是,PHP 团队尚未对此进行记录,而且我仍停留在 PHP 5.3 中,因此无法对此发表评论。

        【讨论】:

        • 在相关说明中,我刚刚发现,如果您正在使用 InnoDB 表,则可以在使用 autocomitt() 方法处理事务时锁定/解锁表,但在以下情况下是不可能的使用 begin_transaction() 方法:MySQL documentation
        • +1 获取详细(和注释)示例以及实际的 mysqli 代码。谢谢你。你关于锁定/交易的观点确实很有趣。
        • “autocommit(FALSE)”是否会影响到同一数据库/表中的另一个连接?我的意思是如果我们打开两个页面,其中一个将其连接设置为“autocommit(FALSE)”,但另一个离开了自动提交功能,它是否等待提交功能。我想知道自动提交是否是连接的属性,而不是数据库/表的属性。谢谢
        • @Hamid $conn-&gt;autocommit(FALSE),在上面的例子中,只影响单个连接 - 它对数据库的任何其他连接没有影响。
        • 注意:如果查询能够返回评估为假或零的有效结果,则应该使用if (result === false) 而不是if (!result)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多