Handling of MySQL's transactions

Source: Internet
Author: User

Steps:

1. Turn on transaction start transaction

When we start a transaction, our operations on SQL occur in memory, but there is no real feedback to the files in the database disk!

2. Rollback rollback

Rollback is the most primitive state to revert to before the transaction is opened!

Note: The rollback operation automatically closes a transaction, and if you want to execute the transaction again, you need to reopen the transaction!

3. Commit a commit

Fundamentals of Business

Ordinary execution, the reason is immediately executed and effective, because the default, MySQL SQL statement execution is automatically submitted! So, the essence of opening a transaction is to turn off the functionality of the previous autocommit, but rather the user commits it manually (using the COMMIT statement)!

Steps to summarize a transaction:

1, open the transaction

2, if execution succeeds, commit commit

3, if any one of the SQL statements fails to execute, roll back the rollback!

The most typical thing about transaction processing is borrowing money. the following take Zhang San to John Doe also 1000 Yuan for example

First look at the amount of money in the database

Here's the code for handling the money transaction:

<?PHP/** * MySQL implements transactional operations*/Echo"<meta charset=utf-8>";//1 connecting the database$link= @mysql_connect(' localhost ', ' root ', ') or die(' Failed to connect to database ');mysql_select_db(' Test ',$link);mysql_query(' Set names UTF8 ');//2 Opening a transactionmysql_query("Start Transaction");//set a variable to determine if all SQL statements are successful$flag=true;//2.1 Executing a set of SQL statements in a transaction
John Doe's money+1000$sql= "Update PDO set mone=money+1000 where name= ' John Doe '";$res=mysql_query($sql);if(!$res) { //If SQL statement execution fails, set $falg to False $flag=false;}//Zhang San's money-1000$sql= "Update PDO set money=money-1000 where Name= ' Zhang San '";$res=mysql_query($sql);if(!$res) { //If SQL statement execution fails, set $falg to False $flag=false;}//2.2 To determine if a transaction is performing successfullyif($flag) { //all SQL statements execute successfully and SQL statements are submitted mysql_query(' Commit '); Echo"The money is still successful!" ";}Else{ //if one of the executions fails, roll back to the state before the transaction is opened mysql_query(' Rollback '); Echo"The money has failed!" ";}

Results:

Below, we deliberately write one of the fields wrong to see if the transaction is normal processing, the amount of money in the database has changed!

// John Doe's money+1000 $sql = "Update PDO set mone=money+1000 where name= ' John Doe '";  // mistakenly write the Moeny field Mone

Results:

The result is that the money has failed, and the amount of the data in the database has not changed, indicating that when a statement is not executed successfully, the thing will not commit, but will roll back, and return to the original state before the start of the transaction, which is also the use of transactions, that is, only when all the SQL statements in the transaction successfully executed, The transaction will not be committed, or it will be rolled back!

The next chapter describes the transaction processing in PDO

Handling of MySQL's transactions

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.