This article mainly introduces the PHP Method for operating MySQL transactions, analyzes ACID features in a more detailed manner in the form of instances, and has good reference value. If you need it, you can refer
This article mainly introduces the PHP Method for operating MySQL transactions, analyzes ACID features in a more detailed manner in the form of instances, and has good reference value. If you need it, you can refer
This example describes how to operate MySQL transactions in PHP. The specific method is as follows:
Generally, transactions must have ACID characteristics. ACID is the first letter of four words: Atomic (Atomicity), Consistent (consistency), Isolated (isolation), and Durable (continuity, the following uses bank transfers as an example to describe their meanings:
① Atomicity: the statements that constitute Transaction Processing Form A logical unit and cannot only execute a part of them. In other words, transactions are the smallest units that are inseparable. For example, in the bank transfer process, it is unreasonable to change only one account by subtracting the transfer amount from one account and adding it to another account.
② Consistency: the database is consistent before and after transaction processing. That is to say, the transaction should correctly switch the system status. For example, in the bank transfer process, either the transfer amount is transferred from one account to another, or both accounts remain unchanged.
③ Isolation: one transaction has no impact on the processing of another transaction. That is to say, no transaction can see a transaction in an incomplete state. For example, in the bank transfer process, another transfer transaction can only be in the waiting state before the transfer transaction is committed.
④ Continuity: the effect of transaction processing can be permanently saved. Conversely, transactions should be able to withstand all failures, including server, process, communication, and media failures. For example, in the bank transfer process, the account status must be saved after the transfer.
In PHP, mysqli has well encapsulated mysql transaction-related operations. Example:
The Code is as follows:
$ Sql1 = "update User set ScoreCount = ScoreCount + 10 where ID = '000000 '";
$ Sql2 = "update ScoreDetail set FScore = 300 where ID = '000000 '";
$ Sql3 = "insert into ScoreDetail ID, Score) values ('000000', 60 )";
$ Mysqli = new mysqli ('localhost', 'root', '', 'db _ Lib2Test ');
$ Mysqli-> autocommit (false); // start transaction
$ Mysqli-> query ($ sql1 );
$ Mysqli-> query ($ sql2 );
If (! $ Mysqli-> errno ){
$ Mysqli-> commit ();
Echo 'OK ';
} Else {
Echo 'err ';
$ Mysqli-> rollback ();
}
Here, we use php mysql functions to execute transactions.
The Code is as follows:
$ Sql1 = "update User set ScoreCount = ScoreCount + 10 where ID = '000000 '";
$ Sql2 = "update ScoreDetail set FScore = 300 where ID = '000000 '";
$ Sql3 = "insert into ScoreDetail ID, Score) values ('000000', 60 )";
$ Conn = mysql_connect ('localhost', 'root ','');
Mysql_select_db ('db _ Lib2Test ');
Mysql_query ('start transaction ');
// Mysql_query ('set autocommit = 0 ');
Mysql_query ($ sql1 );
Mysql_query ($ sql2 );
If (mysql_errno ()){
Mysql_query ('rollback ');
Echo 'err ';
} Else {
Mysql_query ('commit ');
Echo 'OK ';
}
// Mysql_query ('set autocommit = 1 ');
// Mysql_query ($ sql3 );
Note:
MyISAM: transactions are not supported and used by read-only programs to improve performance.
InnoDB: supports ACID transactions, row-level locks, and concurrency.
Berkeley DB: supports transactions.
I hope this article will help you design the PHP + MySQL database program.