thinkphp transaction rollback processing and original PHP transaction rollback instance, thinkphp instance _php tutorial

Source: Internet
Author: User

thinkphp transaction rollback processing and the original PHP transaction rollback instance, thinkphp instance


1. To support transactions within the program, the first connected database and data table must support the transaction MySQL for example:

Database InnoDB Support Transactions

Data table Support transactions: InnoDB support Transaction

2. Framework thinkphp Support Transaction code

 Public function Testrollback () {$model 1 = D (' item '); $model 2 = D (' vote '); $model 1, Starttrans (); $res 1 $model 1->where (' id = 5 '),Delete (); $res 2 $model 2->where (' id = 2 '),Delete ();d UMP ($res 1);d UMP ($res 2 ); if ($res 1$res 2) {$model 1->commit ();   // only $res1 and  $res 2  are executed successfully in order to actually perform the above database operation dump ("commit");} Else {$model 1->rollback ();  //   Condition not satisfied, rollback dump ("rollback");} Dump ("over"); Exit ;}

3. Original PHP Code Transaction instance

Method One: Only support the database and the data table are InnoDB cases

 Public functionRollbackoriginal1 () {$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 '); $sql 1= "INSERT into ' summer_userdata ' (' uid ', ' type ', ' target_id ') VALUES (41,1,233);"; $sql 2= "INSERT into ' summer_userdata ' (' uid ', ' type ', ' target_id ') VALUES (fdfd,2,235);"; $res 1=mysql_query($sql 1); $res 2=mysql_query($sql 2); Dump ($sql 1); Dump ($sql 2); Dump ($res 1); Dump ($res 2); if($res 1&&$res 2){            mysql_query(' COMMIT '); Dump (' Commit success! '); }Else{            mysql_query(' ROLLBACK '); Dump (' Commit failed, rollback! '); }        mysql_query(' END '); }

Method Two: (Note: The table locking method is available for MyISAM engine databases that do not support transactions)

 Public functionRollbackoriginal2 () {$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 ');////Set MySQL not to submit automatically, you need to commit the commit statement by yourself        $sql 1= "INSERT into ' summer_userdata ' (' uid ', ' type ', ' target_id ') VALUES (41,1,233);"; $sql 2= "INSERT into ' summer_userdata ' (' uid ', ' type ', ' target_id ') VALUES (44,2,235);"; //mysql_query ("Lock TABLES ' Hmbl_userdata ' WRITE");//Lock Table        $res 1=mysql_query($sql 1); $res 2=mysql_query($sql 2); Dump ($sql 1); Dump ($sql 2); Dump ($res 1); Dump ($res 2); //mysql_query ("UNLOCK TABLES");//Unlocked        if($res 1&&$res 2){            mysql_query(' COMMIT '); Dump (' Commit success! '); }Else{            mysql_query(' ROLLBACK '); Dump (' Commit failed, rollback! '); }        mysql_query("SET autocommit=1"); mysql_query(' END '); }

php + MySQL transaction processing is relatively simple, involving multiple data operations in the business, you can consider using transaction processing

http://www.bkjia.com/PHPjc/979001.html www.bkjia.com true http://www.bkjia.com/PHPjc/979001.html techarticle thinkphp Transaction rollback processing and the original PHP transaction rollback instance, thinkphp instance 1. To support transactions inside the program, first connect the database and data table must support the transaction MySQL for example ...

  • 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.