1<?PHP2 Header("Content-type:text/html;charset=utf-8");3 require_once"SqlHelper.class.php";4 5 $sql 1= "Update count set balance=balance-2 where id=1";6 $sql 2= "Update count set balance=balance+2 where id=2";7 $mysqli=NewSqlHelper ();8 //autocommit (false) 9 $mysqli->commit (false);Ten $result 1=$mysqli->EXECUTE_DML ($sql 1); One $result 2=$mysqli->EXECUTE_DML ($sql 2); A if(!$result 1|| !$result 2) { - #code ... - echo "failure, rollback"; $mysqli->commit (back); -}Else{ - echo "Success"; $mysqli->commit (ture); + } - +?>
Tool class
1<?PHP2 3 /**4 * Function Name: sqlHelper.class.php5 * Function Function: mysqli object-oriented dql DML query6 * Function Zhang Zhengui7 * Creation time: 2015-01-058 * Modified time:9 */Ten classSqlHelper One { A Private $mysqli; - Private Static $host= ' localhost '; - Private Static $root= "Root"; the Private Static $password= ""; - Private Static $dbname= "Test"; - Public function__construct () - { + #code ... - $this->mysqli =NewMysqli (self::$host, Self::$root, Self::$password, Self::$dbname); + if($this->mysqli->connect_error) { A #code ... at die("Connection failed.")$this->mysqli->connect_error); - } - $this->mysqli->query ("Set Names UTF8"); - - } - Public functionEXECUTE_DQL ($sql){ in $res=$this->mysqli->query ($sql) or die("Dql failed".)$this->mysqli->error); - return $res; to } + Public functionEXECUTE_DML ($sql){ - $res=$this->mysqli->query ($sql) or die("DML failed".$this->mysqli->error); the if(!$res) { * #code ... Failed $ return0;Panax Notoginseng}ElseIf($this->mysqli->fetch_rows > 0) { - #code ... Success the return1; +}Else{ A #code ... Does not affect the number of rows the return2; + } - } $ Public function Commit ($b) {$b ==false) {# code...//transaction rollback $this- >mysqli->autocommit (false);}elseif ($b ==true) {# code ... Wuyi $this->mysqli->commit ();}elseif ($b ==back) {# # code ... Wu $this->mysqli->rollback ();}56} $ } -?>
My transaction rollback