MySQL transaction processing

Source: Internet
Author: User

MySQL transaction processing (the engine of the table must be INNODB/BDB)
There are two main types of two methods: the first is recommended
1. Using Begin,rollback,commit to achieve
Begin a transaction
ROLLBACK TRANSACTION Rollback
Commit TRANSACTION Commit

2. Directly using set to change the MySQL automatic submission mode, the system is automatically submitted by default
Set autocommit = 0; Prohibit Auto-commit
Set autocommit = 1; Turn on auto-commit

This is done in the first way:
<?php
/*
Table Information InnoDB (id,text)
*/
Require_once (' conn.php '); Database connection Information
$flag = 1; Identifies whether to complete the commit or rollback 1. Commit complete 0. Failed rollback
Because it is autocommit (the default), you must start the transaction manually and manually commit the transaction if you want to use it.
Open transaction
mysql_query ("BEGIN"); or mysql_query ("START TRANSACTION");
$sql 1= "INSERT into InnoDB values (', ' xxx ')";
$sql 2 = "INSERT into InnoDB values (' ', ' iiii ')";
$sql 3 = "INSERT into InnoDB values (', ' eee ' ee ')";
if (!mysql_query ($sql 1)) {$flag = 0;}

if (!mysql_query ($sql 2)) {$flag = 0;}
if (!mysql_query ($sql 3)) {$flag = 0;}

if ($flag)
{
mysql_query ("commit");
echo "Commit";
}else
{
mysql_query ("rollback");
echo "rollback";
}
Because $sql 3 is wrong, it is not possible to insert the above data
To end this business.
mysql_query ("END");
----------------
The second method is available:
<?php
Require_once (' conn.php '); Database connection Information
Equivalent to opening a transaction, preventing auto-commit
mysql_query ("Set autocommit=0", $conn);
$flag = 1; Identifies whether to complete the commit or rollback 1. Commit complete 0. Failed rollback
/*
Table Information InnoDB (id,text)
*/
$sql 1= "INSERT into InnoDB values (', ' xxx ')";
$sql 2 = "INSERT into InnoDB values (' ', ' iiii ')";
$sql 3 = "INSERT into InnoDB values (', ' eee ' ee ')";
if (!mysql_query ($sql 1)) {$flag = 0;}

if (!mysql_query ($sql 2)) {$flag = 0;}
if (!mysql_query ($sql 3)) {$flag = 0;}

if ($flag)
{
mysql_query ("commit");
echo "Commit";
}else
{
mysql_query ("rollback");
echo "rollback";
}

mysql_query ("END", $conn); End transaction
mysql_query ("Set Autocommit=1", $conn); Revert auto Commit, cancel transaction
Because $sql 3 is wrong, it is not possible to insert the above data
/* Must be committed or rolled back, to invoke mysql_query ("END", $conn), and mysql_query ("Set Autocommit=1", $conn), or the subsequent query will be treated as a transaction, do not commit when not executed

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.