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 $model 1 = d (' item '$model 2 = d (' vote '$model 1 , $res 1 $model 1->where (' id = 5 ')$res 2$model 2->where (' id = 2 ') Delete ();d UMP ($res 1);d UMP ($res 2if($res 1 $res 2$model 1// only $res1 and $res 2 All execution success is just the actual execution of the above database operation dump ("commit");} Else $model 1// 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
The above describes the transaction rollback processing of thinkphp and the original PHP transaction rollback instance, including the contents of the content, I hope that the PHP tutorial interested in a friend helpful.