PHP and MySQL atomic operations

Source: Internet
Author: User

I haven't written any learning logs for a long time, and I don't know what I 've been busy with recently. I suddenly felt I should write something.

The database's atomic operation was learned two or three months ago. It was suddenly used again today, so it must be recorded today, or it will be searched again next time.

A common method for atomic operations is to implement data rollback. Using PHP to implement database rollback is quite simple:

1. Establish a database connection
2, mysql_query ('begin'); start the transaction
3, $ SQL = "...";
Mysql_query ($ SQL); perform database operations
4. Determine the rollback conditions:
If (mysql_errno)
{
Print mysql_error ();
Mysql_query ('rollback'); rollback if an error occurs
Exit ();
}
5 ,... you can repeat the operations in step 3 and Step 4 to start the process (other operations can be added in the middle, not limited to database updates, but note that it is best not to make a transaction take too long, because it locks all the tables you use, it will affect the use of other programs)
You can also intentionally write an incorrect statement after several correct SQL update statements to check whether the statement is rolled back.
6. End the rollback operation.
Mysql_query ('commit '); here, it indicates that all the above database operations are correct and are officially submitted for execution.

This is the entire process of using PHP to implement atomic operations. It is important to note that a table structure supporting data rollback is established. In addition, there are other methods besides commit to end the rollback operation, but I don't know.

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.