These days to pay the time, and the use of things, in order to facilitate their own later to view, today idle nothing on the previous things to tidy up. (Which references someone else's stuff, here thanks to the code they contributed!) )
First, transaction processing overview:
Transaction: is a collection of several events
Transaction processing: When all events execute successfully, the transaction executes, and if any of the events fail to execute successfully, the other events of the transaction are not executed.
As long as your MySQL version supports BDB or InnoDB table types, your MySQL has the ability to handle transactions. In this, the InnoDB table type is used most, the following is the InnoDB table type as an example of the MySQL transaction processing.
There are two main methods of MySQL transaction processing.
1, with Begin,rollback,commit to achieve
Begin a transaction
ROLLBACK TRANSACTION Rollback
Commit TRANSACTION Acknowledgement
2, directly with set to change the MySQL automatic submission mode
MySQL is automatically submitted by default, that is, you submit a query, it is executed directly! We can pass
Set autocommit=0 prohibit auto-commit
Set autocommit=1 turn on auto-commit
———————————————————————————————————————————————————————————————————————————————
things in PHP for PDO:(Here I'm just tidying up, quoting the original address: http://www.poluoluo.com/jzxy/201410/315255.html)
Try{$pdo=NewPDO ("Mysql:host=localhost;dbname=psp", "Root", "" "); $pdo-exec("Set Names UTF8"); $pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception);//Set exception handling mode $pdo->setattribute (pdo::attr_autocommit,0);//Turn off auto-commit}Catch(pdoexception$e) {Echo"Database connection Failed"; Exit; }Try{$age=10; $pdo->begintransaction ();//Start a transaction $affected _rows1=$pdo-exec("Update Kfry set k_age=k_age+{$age} where k_name= ' User1 ' "); $affected _rows2=$pdo-exec("Update Kfry set k_age=k_age-{$age} where k_name= ' User2 ' ");//change to make it perform successfully or fail /*if ($affected _rows1&& $affected _rows2) {$pdo->commit (); Echo "Operation succeeded"; }else{$pdo->rollback (); } */ if(!$affected _rows1)Throw NewPdoexception ("Add Error"); if(!$affected _rows2)Throw NewPdoexception ("Reduce errors"); Echo"Operation succeeded"; $pdo->commit ();//if execution succeeds to the previous two update SQL statement execution, the entire transaction executes successfully}Catch(pdoexception$e) {Echo"Operation failed:".$e-GetMessage (); $pdo->rollback ();//The statement in the execution transaction is out of the question and the entire transaction is undone}$pdo->setattribute (pdo::attr_autocommit,1);
Things in MySQL: (Self-organizing others, original address: https://my.oschina.net/u/162418/blog/314498)
<?PHP//Database Connection$conn=mysql_connect(' localhost ', ' root ', ');mysql_select_db(' Test ',$conn);mysql_query("SET NAMES GBK");/*the table that supports transactions must be INNODB type only once in a transaction: mysql_query (' Start TRANSACTION ');//Start Transaction mysql_query (' ROLLBACK ');//ROLLBACK TRANSACTION Mysql_ Query (' commit ');//Commit a transaction if there are multiple rollback transactions in a transaction, when the transaction is committed, all operations on the database are canceled before the first rollback to the start of the transaction, and all the database operations are still valid until the transaction is committed before the first rollback. Therefore, the rollback statement is generally placed only before committing a transaction statement if there is no commit statement for a transaction, all of the following database operations from the beginning of the transaction are executed (the execution method returns the right error), but there is no impact on the database, but the previous transaction is automatically committed when the next segment starts the transaction statement*/mysql_query(' START TRANSACTION ');$isBad= 0;$ins _testtable1= "INSERT into Testtable1 (name,age) VALUES (' first ', 23)";if(!mysql_query($ins _testtable1)){ $isBad=1;}//error inserting statement field name$ins _testtable2= "INSERT into Testtable1 (name,ages) VALUES (' second ', ' 24 ')";if(!mysql_query($ins _testtable2)){ $isBad=1;}if($isBad= = 1){ Echo $isBad; mysql_query(' ROLLBACK ');}mysql_query(' COMMIT ');Mysql_close($conn);?>
PHP and MySQL things handling