Php combines mysql with mysqli to expand the transaction processing method, mysqlmysqli

Source: Internet
Author: User

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 !)

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.