The conditions for realizing this function are the environment mysql 5.2/php 5. The table type that supports transactions requires InnoDB. With these conditions, you can implement the above. This transaction rollback operation is often used in large projects. It will be used in banks, e-commerce, etc. Friends in need can refer to it.
Recently, the project software has been upgraded to support transaction processing. I will give you an example for your reference
Environment mysql 5.2/php 5
Table types that support transactions require InnoDB
The code is as follows
代码如下 |
复制代码 |
$LinkID =mysql_connect('localhost:3307','root',*******);
mysql_select_db('web_his',$LinkID);
mysql_query("set names utf8");
/* 创建事务 */
mysql_query('START TRANSACTION') or exit(mysql_error());
$ssql1="insert into pf_item values('22','我们','30')"; //执行sql 1
if(!mysql_query($ssql1)){
echo $ssql1.mysql_errno().":".mysql_error()." ";
mysql_query('ROLLBACK') or exit(mysql_error());//判断当执行失败时回滚
exit;
}
$ssql1="insert into pf_item values('21','hell','10')"; //执行sql 2
if(!mysql_query($ssql1)){
echo $ssql1.mysql_errno().":".mysql_error()." ";
mysql_query('ROLLBACK') or exit(mysql_error());//判断当执行失败时回滚
exit;
}
mysql_query('COMMIT') or exit(mysql_error());//执行事务
mysql_close($LinkID);
?>
|
|
Copy code |
|
mysql_select_db('web_his',$LinkID);
mysql_query("set names utf8");
/* Create transaction */
mysql_query('START TRANSACTION') or exit(mysql_error());
$ssql1="insert into pf_item values('22','we','30')"; //Execute sql 1
if(!mysql_query($ssql1)){
echo $ssql1.mysql_errno().":".mysql_error()."
";
Mysql_query('ROLLBACK') or exit(mysql_error());//Determine rollback when execution fails
exit;
}
$ssql1="insert into pf_item values('21','hell','10')"; //Execute sql 2
if(!mysql_query($ssql1)){
echo $ssql1.mysql_errno().":".mysql_error()."
";
Mysql_query('ROLLBACK') or exit(mysql_error());//Determine rollback when execution fails
}
mysql_query('COMMIT') or exit(mysql_error());//Execute transaction
mysql_close($LinkID);
?>
http://www.bkjia.com/PHPjc/632945.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632945.htmlTechArticleThe conditions for realizing this function are the environment mysql 5.2/php 5. The table type that supports transactions requires InnoDB. With these Under the conditions, you can implement the above. This rollback operation is a big deal...