Home  >  Article  >  Backend Development  >  thinkphp transaction rollback processing and original PHP transaction rollback example

thinkphp transaction rollback processing and original PHP transaction rollback example

WBOY
WBOYOriginal
2016-08-08 09:26:39888browse

1. To support transactions in the program, the database and data table connected must first support transactions mysql. For example:

Database InnoDB supports transactions

Data tables support transactions: InnoDB supports transactions

2. Framework thinkphp supports transaction code

public function testrollback(){ $model1 = D('item'); $model2 = D('vote'); $model1->startTrans(); $res1 = $model1->where('id = 5')->delete(); $res2 = $model2->where('id = 2')->delete();
dump($res1);
dump($res2); if($res1 && $res2){ $model1->commit(); //只有$res1 和  $res2  都执行成功是才真正执行上面的数据库操作 dump("commit");
}else{ $model1->rollback(); // 条件不满足,回滚 dump("rollback");
}
dump("over"); exit;
}

3. Original PHP code transaction example

Method 1: Only supports the case where the database and data table are both innoDB

public function rollbackoriginal1(){ $conn = mysql_connect('127.0.0.1','summerzi','summerzi') or die('DB connection failed!'); mysql_select_db('summer',$conn); mysql_query('set names "GBK"'); mysql_query('BEGIN'); $sql1 = "INSERT INTO `summer_userdata`(`uid`,`type`,`target_id`) VALUES(41,1,233);"; $sql2 = "INSERT INTO `summer_userdata`(`uid`,`type`,`target_id`) VALUES(fdfd,2,235);"; $res1 = mysql_query($sql1); $res2 = mysql_query($sql2);
        dump($sql1);
        dump($sql2);
        dump($res1);
        dump($res2); if($res1 && $res2){ mysql_query('COMMIT');
            dump('commit success!');
        }else{ mysql_query('ROLLBACK');
            dump('commit failed, rollback!');
        } mysql_query('END');

    }

Method 2: (Note: For MyISAM engine databases that do not support transactions, you can use the table locking method)

public function rollbackoriginal2(){ $conn = mysql_connect('127.0.0.1','summerzi','summerzi') or die('DB connection failed!'); mysql_select_db('summer',$conn); mysql_query('set names "GBK"'); mysql_query('SET AUTOCOMMIT=0');////设置mysql不自动提交,需自行用commit语句提交 $sql1 = "INSERT INTO `summer_userdata`(`uid`,`type`,`target_id`) VALUES(41,1,233);"; $sql2 = "INSERT INTO `summer_userdata`(`uid`,`type`,`target_id`) VALUES(44,2,235);"; //mysql_query("LOCK TABLES `hmbl_userdata` WRITE");//锁定表 $res1 = mysql_query($sql1); $res2 = mysql_query($sql2);
        dump($sql1);
        dump($sql2);
        dump($res1);
        dump($res2); //mysql_query("UNLOCK TABLES");//解除锁定 if($res1 && $res2){ mysql_query('COMMIT');
            dump('commit success!');
        }else{ mysql_query('ROLLBACK');
            dump('commit failed, rollback!');
        } mysql_query("SET AUTOCOMMIT=1"); mysql_query('END');
        

    }

php + mysql is relatively simple to process transactions. If it involves multiple data operations in the business, you can consider using transaction processing.


The above introduces the transaction rollback processing of thinkphp and the transaction rollback example of original PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php file upload limitNext article:php file upload limit