In fact, it is not difficult to use PHP to process mysql transaction rollback. the following small series will introduce it to you in detail. I believe that after reading this article, we all know how to use a lot of new users in the project process, such as: in the forum coin deduction project, when a user makes a payment for the Forum currency, if the network is suddenly disconnected, the computer crashes, power outages, and other natural disasters, the transaction fails (that is, the user's currency has been deducted, but there are no consumption records in the server database and other situations). how should we handle this situation?
At this time, we can use Mysql transaction rollback for processing. how can we write the code?
Now let me talk about how to handle the mysql transaction rollback.
First, only INNODB and BDB data tables in MYSQL can support transaction processing! Other types are not supported!
What should we do if our data table already exists and is not the two types mentioned above?
1. I can find a software named MySQL-Front, which can change the table type.
2. you can also use SQL statements to modify the statement. The SQL statement can be written as follows:
The code is as follows:
Alter table tablename type = InnoDB;
After changing all the tables to be repaired, we can use the code in the PHP file for testing.
The code is as follows:
Mysql_query ("BEGIN"); // or mysql_query ("start transaction ");
$ SQL = "INSERT ...";
$ Sql2 = "insert ...";
$ Res = mysql_query ($ SQL );
$ Res1 = mysql_query ($ sql2 );
If ($ res & $ res1 ){
Mysql_query ("COMMIT ");
Echo 'submission successful. ';
} Else {
Mysql_query ("ROLLBACK ");
Echo 'data rollback. ';
}
Mysql_query ("END ");
Here, we should know how to use PHP to handle mysql transaction rollback. Easy!