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.
When you use Set autocommit=0, all of your later SQL will be transacted until you end with a commit or rollback.
Note that when you end this business, you start a new business! 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!
: General MySQL database default engine is MyISAM, this engine does not support transactions! If you want MySQL to support transactions, you can modify them manually:
The method is as follows:
1. Modify the C:\appserv\mysql\my.ini file, find the Skip-innodb, add the # in front, and save the file.
2. In operation input: services.msc, restart the MySQL service.
3. In phpMyAdmin, mysql->show engines; (or perform mysql->show variables like ' have_% '; To see InnoDB as yes, which means that the database supports InnoDB.
It also means that support services are transaction.
4. When you create a table, you can select the InnoDB engine for storage engine. If you have previously created a table, you can use the Mysql->alter table table_name Type=innodb;
or Mysql->alter table table_name Engine=innodb to change the engine of the datasheet to support transactions.
/* Method A/*
Copy Code code as follows:
/*************** transaction--1 ***************/
$conn = mysql_connect (' localhost ', ' root ', ' root ') or Die ("Data connection error!!!");
mysql_select_db (' Test ', $conn);
mysql_query ("Set names ' GBK '"); Use GBK Chinese code;
Start a transaction
mysql_query ("BEGIN"); or mysql_query ("START TRANSACTION");
$sql = "INSERT into ' user ' (' id ', ' username ', ' sex ') VALUES (NULL, ' test1 ', ' 0 ')";
$sql 2 = "INSERT into ' user ' (' Did ', ' username ', ' sex ') VALUES (NULL, ' test1 ', ' 0 ')"; I wrote the wrong one on purpose.
$res = mysql_query ($sql);
$res 1 = mysql_query ($sql 2);
if ($res && $res 1) {
mysql_query ("COMMIT");
Echo ' submitted successfully. ';
}else{
mysql_query ("ROLLBACK");
echo ' data rollback. ';
}
mysql_query ("End");
/ * Method Two * *
Copy Code code as follows:
/**************** transaction--2 *******************/
mysql_query ("SET autocommit=0"); Set MySQL does not automatically submit, you need to submit with a commit statement
$sql = "INSERT into ' user ' (' id ', ' username ', ' sex ') VALUES (NULL, ' test1 ', ' 0 ')";
$sql 2 = "INSERT into ' user ' (' Did ', ' username ', ' sex ') VALUES (NULL, ' test1 ', ' 0 ')"; I wrote the wrong one on purpose.
$res = mysql_query ($sql);
$res 1 = mysql_query ($sql 2);
if ($res && $res 1) {
mysql_query ("COMMIT");
Echo ' submitted successfully. ';
}else{
mysql_query ("ROLLBACK");
echo ' data rollback. ';
}
mysql_query ("End"); Do not forget mysql_query ("SET autocommit=1") when the transaction is finished; autocommit