One, MySQL affairs
Only InnoDB types of data tables in MySQL can support transaction processing.
There are two ways to start a transaction
(1) using Begin,rollback,commit to realize
Copy Code code as follows:
Begin a transaction
ROLLBACK TRANSACTION Rollback
Commit TRANSACTION Confirmation
(2)Use set directly to change the automatic submission mode of MySQL
Copy Code code as follows:
Set autocommit=0 prohibit automatic submission
Set autocommit=1 turn on automatic submission
Demo
Copy Code code as follows:
Header ("Content-type:text/html;charset=utf-8");
Mysql_pconnect ("localhost", "root", "") or Die ("database connection failed");
mysql_select_db ("test");
mysql_query ("Set names UTF8");
Open a transaction
mysql_query ("BEGIN");
mysql_query ("START TRANSACTION");
mysql_query ("Set autocommit=1");/set transaction does not automatically commit MySQL default is autocommit
mysql_query ("SET autocommit=1");/Open transaction
$sql 1 = "INSERT into ' test ' values (' 2222 ', ' testing data ')";
$sql 2 = "INSERT into ' test ' values (' $ ', ' sss ', ' 22 ')";/specially written error
$res 1 = mysql_query ($sql 1);
$res 2 = mysql_query ($sql 2);
if ($res 1 && $res 2)
{
mysql_query ("COMMIT");
echo "Transaction submission";
}else{
mysql_query ("ROLLBACK");
echo "Transaction rollback";
}
mysql_query ("End");