首页 > 代码库 > my事务回滚
my事务回滚
1 <?php 2 header("Content-Type:text/html;charset=utf-8"); 3 require_once "sqlHelper.class.php"; 4 5 $sql1 = "update count set balance=balance-2 where id=1"; 6 $sql2 = "update count set balance=balance+2 where id=2"; 7 $mysqli = new SqlHelper(); 8 //autocommit(false) 9 $mysqli->commit(false);10 $result1 = $mysqli->execute_dml($sql1);11 $result2 = $mysqli->execute_dml($sql2);12 if (!$result1 || !$result2) {13 # code...14 echo "失败,回滚";15 $mysqli->commit(back);16 }else{17 echo "成功";18 $mysqli->commit(ture);19 }20 21 ?>
工具类
1 <?php 2 3 /** 4 * 函数名称:sqlHelper.class.php 5 * 函数功能:mysqli面向对象进行dql dml 查询 6 * 函数作者:张真贵 7 * 创建时间:2015-01-05 8 * 修改时间: 9 */10 class SqlHelper11 {12 private $mysqli;13 private static $host = ‘localhost‘;14 private static $root = "root";15 private static $password = "";16 private static $dbname = "test";17 public function __construct()18 {19 # code...20 $this->mysqli = new mysqli(self::$host,self::$root,self::$password,self::$dbname);21 if ($this->mysqli->connect_error) {22 # code...23 die("连接失败".$this->mysqli->connect_error);24 }25 $this->mysqli->query("set names utf8");26 27 }28 public function execute_dql($sql){29 $res = $this->mysqli->query($sql) or die("dql失败".$this->mysqli->error);30 return $res;31 }32 public function execute_dml($sql){33 $res = $this->mysqli->query($sql) or die("dml失败".$this->mysqli->error);34 if (!$res) {35 # code...失败36 return 0;37 }elseif ($this->mysqli->fetch_rows > 0) {38 # code...成功39 return 1;40 }else{41 # code...没有影响行数42 return 2;43 }44 }45 public function commit($b){46 if ($b==false) {47 # code...//事务回滚48 $this->mysqli->autocommit(false);49 }elseif ($b==true) {50 # code...51 $this->mysqli->commit();52 }elseif ($b==back) {53 # code...54 $this->mysqli->rollback();55 }56 }57 }58 ?>
my事务回滚
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。