Php combines mysql with mysqli to expand the transaction processing method, mysqlmysqli
This example describes how php combines mysql with mysqli extension to process transactions. We will share this with you for your reference. The details are as follows:
The following only shows how to apply the application. When using the application, add a judgment. If all operations are successful, submit the application. Otherwise, roll back the application.
Before reading this document, we should first clarify that mysqli and mysql extensions are different.
Mysqli extension processing:
$ Mysqli = new mysqli ('localhost', 'root', '000000', 'test'); $ mysqli-> autocommit (false ); // start transaction $ query = "update a set money = money + 30 where a_id = '1'"; $ mysqli-> query ($ query ); $ query = "update B set money = money-30 where B _id = '1'"; $ mysqli-> query ($ query); // $ mysqli-> rollback (); // roll back $ mysqli-> commit (); // submit a transaction $ mysqli-> autocommit (true); // do not use a transaction
Mysql extension processing:
<? Phpmysql_connect ('localhost', 'root', '123456'); mysql_select_db ('test'); mysql_query ('set AUTOCOMMIT = 0 '); // do not automatically submit mysql_query ('begin'); // start the transaction $ query = "UPDATE a SET money = money + 30 WHERE a_id = 1"; mysql_query ($ query ); $ query = "UPDATE B SET money = money-30 WHERE B _id = 1"; mysql_query ($ query); mysql_query ('commit '); // submit // mysql_query ('rollback'); // roll back mysql_query ('set AUTOCOMMIT = 1'); // enable automatic submission?>
For instructions on mysql extended transaction processing, refer to this site for the original article:
Http://www.bkjia.com/article/50944.htm
There are two main methods to process MYSQL transactions.
1. Use begin, rollback, and commit to implement
Start a transaction
Rollback transaction rollback
Commit transaction validation
2. directly use set to change the mysql automatic submission Mode
MYSQL is automatically submitted by default, that is, when you submit a QUERY, It will be executed directly! We can use:
Set autocommit = 0 disable automatic submission
Set autocommit = 1 enable automatic submission
To process the transaction.
Note that when you use set autocommit = 0, all your SQL statements will be processed as transactions until you use commit to confirm or roll back, note that a new transaction is also started when you end the transaction! In the first method, only the current transaction is used!
The first method is recommended!
In MYSQL, only INNODB and BDB Data Tables support transaction processing! Other types are not supported! (Remember !)