Home > php教程 > php手册 > body text

php+mysql事务rollback&commit示例

WBOY
Release: 2016-06-13 12:19:19
Original
1238 people have browsed it

mysql_query("BEGIN");//开始一个事务
mysql_query("SET AUTOCOMMIT=0"); //设置事务不自动commit
$insert="INSERT INTO userinfo VALUES ('aa12','aa','1','aaa')";
mysql_query($insert);
mysql_query("COMMIT");//非autocommit模式,必须手动执行COMMIT使操作生效
//mysql_query("SET AUTOCOMMIT=0");
$insert="INSERT INTO userinfo VALUES ('aa20','aa','1','aaa')";
$insert="INSERT INTO userinfo VALUES ('aa8','aa','1')";
//mysql_query("COMMIT");
if(mysql_num_rows==0)
mysql_query("ROLLBACK");//非autocommit模式,执行ROLLBACK使事务操作无效
else echo "ok";
//mysql_query("CLOSE");
mysql_query("SET AUTOCOMMIT=1");//恢复autocommit模式
$insert="INSERT INTO userinfo VALUES ('aa15','aa','1','aaa')";
mysql_query($insert);//不需要手动执行COMMIT就可以使事务操作生效

执行BEGIN之后,其作用同set autocommit=0,而且之后设置set autocommit=0或1时无效。所以,为使操作清楚,一般不使用BEGIN。

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template