码迷,mamicode.com
首页 > 其他好文 > 详细

my事务回滚

时间:2015-01-06 11:55:11      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

 

 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 SqlHelper
11     {
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事务回滚

标签:

原文地址:http://www.cnblogs.com/zzg521/p/4205580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!