Home  >  Article  >  Backend Development  >  A brief analysis of the solution to Mysql data rollback error_PHP tutorial

A brief analysis of the solution to Mysql data rollback error_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:00:18980browse

There are two main methods of transaction processing in MYSQL.
1. Use begin, rollback, and commit to implement
begin starts a transaction
rollback transaction rollback
commit transaction confirmation

2. Use set directly to change the automatic submission mode of mysql
MYSQL automatically submits by default, that is, if you submit a QUERY, it will be executed directly! We can implement transaction processing by
set autocommit=0 to disable automatic submission
set autocommit=1 and enable automatic submission
.

When you use set autocommit=0, all your subsequent SQL will be processed as transactions until you confirm with commit or rollback.

Note that when you end this transaction, you also start a new transaction! According to the first method, only the current one is used as a transaction!
I personally recommend using the first method!

Only INNODB and BDB type data tables in MYSQL can support transaction processing! Other types are not supported!
***: Generally, the default engine of MYSQL database is MyISAM. This engine does not support transactions! If you want MYSQL to support transactions, you can modify it manually:

The method is as follows:
1. Modify the c:appservmysqlmy.ini file, find skip-InnoDB, add # in front, and save the file.

2. Enter: services.msc during operation to restart the mysql service.

3. Go to phpmyadmin, mysql->show engines; (or execute mysql->show variables like 'have_%';), check InnoDB for YES, which means the database supports InnoDB.
This means that transaction transactions are supported.

4. When creating a table, you can select the InnoDB engine for the Storage Engine. If it is a previously created table, you can use mysql->alter table table_name type=InnoDB;
or mysql->alter table table_name engine=InnoDB; to change the engine of the data table to support transactions.
/*Method 1*/

Copy code The code is 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 encoding;
//Start a transaction
mysql_query("BEGIN"); //Or mysql_query("START TRANSACTION") ;
$sql = "INSERT INTO `user` (`id`, `username`, `sex`) VALUES (NULL, 'test1', '0')";
$sql2 = "INSERT INTO ` user` (`did`, `username`, `sex`) VALUES (NULL, 'test1', '0')";//I deliberately wrote this wrong
$res = mysql_query($sql);
$res1 = mysql_query($sql2);
if($res && $res1){
mysql_query("COMMIT");
echo 'Submission successful. ';
}else{
mysql_query("ROLLBACK");
echo 'Data rollback. ';
}
mysql_query("END");

/*Method 2*/
Copy Code The code is as follows:

/**************** transaction--2 *******************/
mysql_query("SET AUTOCOMMIT=0"); //Set mysql not to submit automatically, you need to do it yourself Submit with commit statement
$sql = "INSERT INTO `user` (`id`, `username`, `sex`) VALUES (NULL, 'test1', '0')";
$sql2 = " INSERT INTO `user` (`did`, `username`, `sex`) VALUES (NULL, 'test1', '0')";//I deliberately wrote this wrong
$res = mysql_query($sql );
$res1 = mysql_query($sql2);
if($res && $res1){
mysql_query("COMMIT");
echo 'Submit successfully. ';
}else{
mysql_query("ROLLBACK");
echo 'Data rollback. ';
}
mysql_query("END"); //Don't forget to mysql_query("SET AUTOCOMMIT=1"); automatically submit when the transaction is completed

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328082.htmlTechArticleThere are two main methods of transaction processing in MYSQL. 1. Use begin, rollback, and commit to implement begin to start a transaction rollback transaction rollback commit transaction confirmation 2. Use set directly to change mysql...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn