dongpang1898 2016-03-27 23:05
浏览 146
已采纳

如何使用MySQLi事务回滚和提交?

I seem to be having trouble with the rollback feature in PHP7. Here's my code, but if I supply the queries and put an error into one of them, it won't roll back. It still commits everything that had been completed.

function sql_transaction(...$queries) {
    global $db;

    $success = 1;
    $db->autocommit(FALSE);

    foreach($queries as $query) {
        $result = $db->query($query);
        echo $result;
        if(!$result) {
            $success = 0;
            break;
        }
    }

    if($success == 1) {
        $db->commit();
    }else{
        $db->rollback();
    }
    $db->autocommit(TRUE);
    return $success;
}
  • 写回答

1条回答 默认 最新

  • dtzhfb4869 2016-03-27 23:13
    关注

    You've to start transaction before doing this.

    Because You must tell to database that You're going to start transaction.

    You've to put: $db->begin_transaction(); after autocommit(FALSE);

    Please read documentation: mysqli::begin_transaction



    P.S. Please keep in mind it cannot be done with tables that engine does not support transactions. So if after adding begin_transaction statement rollback() did not work, check Your tables engine that it's set to engine with transaction support.

    To check Your tables engine call query in mysql terminal:

    SHOW TABLE STATUS FROM database_name_goes_here;
    

    You'll get list of tables in Your database with engines defined.

    To get list of transaction-safe engines You can do by calling query in mysql terminal (find Transactions: YES):

    mysql> SHOW ENGINES\G
    *************************** 1. row ***************************
          Engine: PERFORMANCE_SCHEMA
         Support: YES
         Comment: Performance Schema
    Transactions: NO
              XA: NO
      Savepoints: NO
    *************************** 2. row ***************************
          Engine: InnoDB
         Support: DEFAULT
         Comment: Supports transactions, row-level locking, and foreign keys
    Transactions: YES
              XA: YES
      Savepoints: YES
    *************************** 3. row ***************************
          Engine: MRG_MYISAM
         Support: YES
         Comment: Collection of identical MyISAM tables
    Transactions: NO
              XA: NO
      Savepoints: NO
    *************************** 4. row ***************************
          Engine: BLACKHOLE
         Support: YES
         Comment: /dev/null storage engine (anything you write to it disappears)
    Transactions: NO
              XA: NO
      Savepoints: NO
    *************************** 5. row ***************************
          Engine: MyISAM
         Support: YES
         Comment: MyISAM storage engine
    Transactions: NO
              XA: NO
      Savepoints: NO
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?