1. mysql transactions
In MYSQL, only INNODB Data Tables support transaction processing.
There are two ways to start a transaction:
(1)Use begin, rollback, and commit to implement
Copy codeThe Code is as follows:
Start a transaction
Rollback transaction rollback
Commit transaction validation
(2)Directly use set to change the automatic mysql submission Mode
Copy codeThe Code is as follows:
Set autocommit = 0 disable automatic submission
Set autocommit = 1 enable automatic submission
Demo
Copy codeThe Code is as follows:
Header ("Content-type: text/html; charset = UTF-8 ");
Mysql_pconnect ("localhost", "root", "") or die ("database connection failed ");
Mysql_select_db ("test ");
Mysql_query ("set names utf8 ");
// Start a transaction
// Mysql_query ("BEGIN ");
// Mysql_query ("start transaction ");
// Mysql_query ("set autocommit = 1"); // sets that the transaction is not automatically committed. mysql is automatically committed by default.
Mysql_query ("set autocommit = 1"); // start the transaction
$ Sql1 = "insert into 'test' values ('20140901', 'test data ')";
$ Sql2 = "insert into 'test' values ('201312', 'ss', '22')"; // specially written Error
$ Res1 = mysql_query ($ sql1 );
$ Res2 = mysql_query ($ sql2 );
If ($ res1 & $ res2)
{
Mysql_query ("COMMIT ");
Echo "transaction commit ";
} Else {
Mysql_query ("ROLLBACK ");
Echo "transaction rollback ";
}
Mysql_query ("END ");