PHP combines MySQL with mysqli to extend processing transactions _php tips

Source: Internet
Author: User
Tags commit extend rollback

This article is an example of how PHP combines MySQL with mysqli to extend processing transactions. Share to everyone for your reference, specific as follows:

The following is just a demonstration of how to apply, the specific use of the time to add judgment, if all executed successfully commits, or rollback

Before you see it, mysqli is different from the MySQL extension.

Mysqli extension handles things:

$mysqli =new mysqli (' localhost ', ' root ', ' 123456 ', ' test ');
$mysqli->autocommit (FALSE);//start things
$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 ();//Rollback
$mysqli->commit ();   Submitting Things
$mysqli->autocommit (true);/no use of things

MySQL extension handles things:

<?php
mysql_connect (' localhost ', ' root ', ' 123456 ');
mysql_select_db (' test ');
mysql_query (' SET autocommit=0 '); Do not automatically submit
mysql_query (' BEGIN ');       Start 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 ');       Submitted to
//mysql_query (' ROLLBACK ');     Rollback
mysql_query (' SET autocommit=1 ');//Turn on autocommit
?>

The description of the MySQL extended processing transaction can be referenced in the original website:

Http://www.jb51.net/article/50944.htm

There are two main ways to handle MySQL transactions.

1, with Begin,rollback,commit to achieve

Begin a transaction
ROLLBACK TRANSACTION Rollback
Commit TRANSACTION Confirmation

2, directly with the set to change the MySQL automatic submission mode

MySQL default is automatically submitted, that is, you submit a query, it directly executed! We can pass:

Set autocommit=0 prohibit automatic submission
Set autocommit=1 turn on automatic submission

To implement transaction processing.

But note that when you use set autocommit=0, all of your later SQL will be transacted, until you end with a commit or rollback, and notice that when you end the transaction, you start a new transaction! The first method will only present as a transaction!

The first method of personal recommendation!

Only InnoDB and BDB types of data tables in MySQL can support transaction processing! Other types are not supported! Remember )

More about PHP Interested readers can view the site topics: "PHP+MYSQLI Database Programming Skills Summary", "PHP based on PDO Operation Database Skills summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", " Introduction to PHP object-oriented programming, "PHP string (String) Usage Summary", "Php+mysql Database Operations Introduction Tutorial" and "PHP Common database Operation tips Summary"

I hope this article will help you with the PHP program design.

Related Article

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.