数据库的事务处理参见另一篇博客,用PDO实现,和MySQLi操作几乎没有差别,地址是http://www.cnblogs.com/-beyond/p/7551177.html
注意要进行事务处理的表格的存储引擎选择Innodb,而非MyISAM
事务处理需要用到以下函数
bool mysqli_begin_transaction ( mysqli $link [, int $flags [, string $name ]] )
bool mysqli_commit ( mysqli $link )
bool mysqli_rollback ( mysqli $link
)
具体事例如下:
<?php $conn=mysqli_connect("localhost","root","root","test"); //开启事务 mysqli_begin_transaction($conn); try{ mysqli_query($conn,"delete from aaa where uid = 99"); //提交操作 mysqli_commit($conn); echo "finished\n"; }catch (Exception $e){ echo "failed\n"; echo $e->getMessage(); //如果出现错误,则撤销开启事务以后的所有操作 mysqli_rollback($conn); } //提交 mysqli_commit($conn); //关闭数据库 mysqli_close($conn); ?>
时间: 2024-11-05 14:33:24