The origin of things:
ProgramWhen you execute two SQL statements at the same time, for example, transfer. If one of the statements fails, the other statement cannot be executed. Otherwise, an error occurs.
PHP uses transactions to solve this problem.
The example is as follows:
<? PHP $ mysqli = new mysqli ("localhost", "root", "root", "test"); // determine if ($ mysqli-> connect_error) {die ($ mysqli-> connect_error);} // set the submission to false $ mysqli-> autocommit (false ); $ sql1 = "update account set balance = balance-2 where id = 1"; $ sql2 = "update account set balance = balance + 2 where id = 2 "; $ b1 = $ mysqli-> query ($ sql1); $ b2 = $ mysqli-> query ($ sql2); If (! $ B1 |! $ B2) {echo "failed, rollback". $ mysqli-> error; // rollback $ mysqli-> rollback ();} else {echo "successful! "; $ Mysqli-> commit () ;}$ mysqli-> close ();?>
Effect description:
If the SQL statement is correct, the execution is successful.
Roll back if the SQL statement is incorrect.
Features of transactions:
Save point case:
Save point: to prevent data loss caused by incorrect operations, save the current status. If the operation fails, you can roll back to this status.
Start transaction:
Start transaction;
Create and save points:
Savepoint A (Save the vertex name );
Operation ------------------
If the operation fails, roll back:
Rollback to;