Home >php教程 >php手册 >php+mysql事务rollback&commit示例

php+mysql事务rollback&commit示例

WBOY
WBOYOriginal
2016-06-13 12:19:191248browse

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。

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