Parsing the transaction rollback operation of phpmysql (with instance)

Source: Internet
Author: User
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!

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.