Home >Backend Development >PHP Tutorial >PHP Mysql Transaction

PHP Mysql Transaction

WBOY
WBOYOriginal
2016-06-23 14:36:16911browse

在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:支持事务
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
Previous article:PHP的ForeachNext article:{php 函数}