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.