PHP Mysql Transaction
程序员文章站
2024-02-08 13:08:04
...
在PHP中,mysqli 已经很好的封装了mysql事务的相关操作。如下示例:
view plain copy to clipboard print ?
$sql1 = "update User set ScoreCount = ScoreCount +10 where ID= '123456'"; $sql2 = "update ScoreDetail set FScore = 300 where ID= '123456'"; $sql3 = "insert into ScoreDetail ID,Score) values ('123456',60)"; $mysqli = new mysqli('localhost','root','','DB_Lib2Test'); $mysqli->autocommit(false);//开始事物 $mysqli->query($sql1); $mysqli->query($sql2); if(!$mysqli->errno){ $mysqli->commit(); echo 'ok'; }else{ echo 'err'; $mysqli->rollback(); }在这里,我们再使用 php mysql 系列函数执行事务。
view plain copy to clipboard print ?
$sql1 = "update User set ScoreCount = ScoreCount +10 where ID= '123456'"; $sql2 = "update ScoreDetail set FScore = 300 where ID= '123456'"; $sql3 = "insert into ScoreDetail ID,Score) values ('123456',60)"; $conn = mysql_connect('localhost','root',''); mysql_select_db('DB_Lib2Test'); mysql_query('start transaction'); //mysql_query('SET autocommit=0'); mysql_query($sql1); mysql_query($sql2); if(mysql_errno ()){ mysql_query('rollback'); echo 'err'; }else{ mysql_query('commit'); echo 'ok'; } // mysql_query('SET autocommit=1'); // mysql_query($sql3);在这里要注意,
MyISAM:不支持事务,用于只读程序提高性能 InnoDB:支持ACID事务、行级锁、并发 Berkeley DB:支持事务上一篇: 求好手,模拟浏览器抓取网页
下一篇: 关于FTP上传文件问题