Transactions in 10-php

Source: Internet
Author: User

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;

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.