Home  >  Article  >  Backend Development  >  Introduction to the implementation method of php transactions (code example)

Introduction to the implementation method of php transactions (code example)

不言
不言forward
2019-02-20 11:33:102857browse

This article brings you an introduction to the implementation method of PHP transactions (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

<?php
$db = new mysqli("localhost","root","","test"); //连接数据库
$db->autocommit(false); //设置为非自动提交——事务处理
$sql1  = "INSERT INTO `test`.`test1` (`name` )VALUES (&#39;1&#39; )";
$result1 = $db->query($sql1);
$sql2  = "INSERT INTO `test`.`test2` (`a` )VALUES (&#39;1&#39;)";
$result2 = $db->query($sql2);
if ($result1 && $result2) {
$db->commit();  //全部成功,提交执行结果
echo &#39;提交&#39;;
} else {
$db->rollback(); //有任何错误发生,回滚并取消执行结果
echo &#39;回滚&#39;;
}
$db->autocommit(true); //设置为非自动提交——事务处理
$db->close();
?>

There 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. Directly use set to change the automatic submission mode of mysql
MYSQL is automatically submitted 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!
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 manually modify it yourself:
The method is as follows:

1. Modify the c:\appserv\mysql\my.ini file, find skip-InnoDB, and add # in front, Then 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_%';), and check that InnoDB is 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.
*/

/*************** transaction--1 ***************/
$conn = mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;root&#39;) or die ("数据连接错误!!!");
mysql_select_db(&#39;test&#39;,$conn);
mysql_query("set names &#39;GBK&#39;"); //使用GBK中文编码;
//开始一个事务
mysql_query("BEGIN"); //或者mysql_query("START TRANSACTION");
$sql = "INSERT INTO `user` (`id`, `username`, `sex`) VALUES (NULL, &#39;test1&#39;, &#39;0&#39;)";
$sql2 = "INSERT INTO `user` (`did`, `username`, `sex`) VALUES (NULL, &#39;test1&#39;, &#39;0&#39;)";//这条我故意写错
$res = mysql_query($sql);
$res1 = mysql_query($sql2); 
if($res && $res1){
mysql_query("COMMIT");
echo &#39;提交成功。&#39;;
}else{
mysql_query("ROLLBACK");
echo &#39;数据回滚。&#39;;
}
mysql_query("END");
/**************** transaction--2 *******************/
/*方法二*/
mysql_query("SET AUTOCOMMIT=0"); //设置mysql不自动提交,需自行用commit语句提交
$sql = "INSERT INTO `user` (`id`, `username`, `sex`) VALUES (NULL, &#39;test1&#39;, &#39;0&#39;)";
$sql2 = "INSERT INTO `user` (`did`, `username`, `sex`) VALUES (NULL, &#39;test1&#39;, &#39;0&#39;)";//这条我故意写错
$res = mysql_query($sql);
$res1 = mysql_query($sql2); 
if($res && $res1){
mysql_query("COMMIT");
echo &#39;提交成功。&#39;;
}else{
mysql_query("ROLLBACK");
echo &#39;数据回滚。&#39;;
}
mysql_query("END"); //事务处理完时别忘记mysql_query("SET AUTOCOMMIT=1");自动提交
/******************对于不支持事务的MyISAM引擎数据库可以使用表锁定的方法:********************/
//MyISAM & InnoDB 都支持,
/*
LOCK TABLES可以锁定用于当前线程的表。如果表被其它线程锁定,则造成堵塞,直到可以获取所有锁定为止。
UNLOCK TABLES可以释放被当前线程保持的任何锁定。当线程发布另一个LOCK TABLES时,或当与服务器的连接被关闭时,所有由当前线程锁定的表被隐含地解锁。
*/
mysql_query("LOCK TABLES `user` WRITE");//锁住`user`表
$sql = "INSERT INTO `user` (`id`, `username`, `sex`) VALUES (NULL, &#39;test1&#39;, &#39;0&#39;)";
$res = mysql_query($sql);
if($res){
echo &#39;提交成功。!&#39;;
}else{
echo &#39;失败!&#39;;
}
mysql_query("UNLOCK TABLES");//解除锁定


The above is the detailed content of Introduction to the implementation method of php transactions (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete