Thinkphp transaction rollback and original PHP transaction rollback instances

Source: Internet
Author: User
: This article mainly introduces thinkphp's transaction rollback process and the original PHP transaction rollback instance. if you are interested in the PHP Tutorial, refer to it. 1. to support transactions in the program, the connected database and data table must support transaction mysql as an example:

Database InnoDB supports transactions

Data tables support transactions: InnoDB supports transaction

2. the thinkphp framework 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 (); // The above database operation dump ("commit") is actually executed only when $ res1 and $ res2 are successfully executed;} else {$ model1-> rollback (); // if the conditions are not met, roll back dump ("rollback");} dump ("over"); exit ;}

3. original PHP code transaction instance

Method 1: only innoDB databases and data tables are supported.

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: You can use the table lock method for MyISAM engine databases that do not support transactions)

Public function rollbackoriginal () {$ conn = mysql_connect ('2017. 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 '); /// if mysql is not automatically submitted, use the commit statement to submit $ sql1 = "insert into 'Summer _ userdata' ('uid', 'type ', 'target _ id') VALUES (41,1, 233); "; $ sql2 =" insert into 'Summer _ userdata' ('uid', 'type ', 'target _ id') VALUES (235,); "; // mysql_query (" lock tables 'hmbl _ userdata' WRITE "); // lock the table $ res1 = mysql_query ($ sql1); $ res2 = mysql _ Query ($ sql2); dump ($ sql1); dump ($ sql2); dump ($ res1); dump ($ res2 ); // mysql_query ("unlock tables"); // UNLOCK 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. when it involves multiple data operations in the business, you can consider using transactions.


The above introduces thinkphp's transaction rollback processing and the original PHP transaction rollback instance, including some content, hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.