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