MySql - InnoDB - 事务 , Php版

(出处:http://www.cnblogs.com/linguanh/)

 

1,前序

  由于要重构APP(社交类) 服务端接口的部分代码,故接触到了 innoDB,以及事务这个词,下面主要是以例子的形式显示它的用法,理论的东西不过多讲述。

 

2,何为事务

  鄙人在编程方面的解释:多条命令操作放在一起处理 , 例如提交的时候一起提交,撤销的时候也是一起撤销,书本对这个词的定义内容很多。

 

3,myisam 与 innoDB  

  它们都是mysql数据库的引擎,我们一般建表默认的是 myisam 引擎,比较:http://www.cnblogs.com/vicenteforever/articles/1613119.html,个人认为,类似于insert、update、delete 这种操作如果涉及多表或单表互联操作的情况,为了避免数据写脏,请使用事务。因为整个过程中若一条错误,便可以回滚到开始时的状态。

 

4,分享个基于php的类

 

 1 <?php
 2 
 3 /**
 4  * Created by PhpStorm.
 5  * User: 林冠宏
 6  * Date: 2016/4/28
 7  * Time: 10:20
 8  */
 9 include "Config.php"; /** 数据库配置信息类,自行完善 */
10 
11 class Sql{
12     public $link    = null;
13     private $config = null;
14     /**
15      * 是否直接开启事务
16      */
17     public function Sql($begin = false){
18         $this->config = new Config();
19         $this->connect();
20         mysql_query("SET AUTOCOMMIT=0",$this->link); /** 设置不自动提交,默认是自动提交 */
21         if($begin){
22             $this->SWBegin();
23         }
24     }
25 
26     public function connect(){
27         $this->link = mysql_connect($this->config->host,$this->config->user,$this->config->pw); /** 连接数据库 */
28         mysql_query("SET NAMES 'utf8'",$this->link); /** 经验总结,使用mysql设置页面编码,最好等链接了,再设置,意思是在连库函数后面使用 */
29 
30         if(!$this->link){
31             exit("connect_dataBase_wrong");
32         }
33         if(!mysql_select_db($this->config->db,$this->link)){
34             exit("select_db_wrong");
35         }
36     }
37 
38     /**
39      * 命令、是否判断行数、出错是否自动启用回滚、链式提交
40      */
41     public function exec($query,$judgeLength=false,$rollBack = false,$isCommit=false){
42         $res = mysql_query($query,$this->link);
43         if($judgeLength){ /** 是否判断行数 */
44             if(mysql_num_rows($res)<=0){
45                 return null;
46             }
47         }else{
48             if(!$res){
49                 if($rollBack) {
50                     $this->rollBack();
51                 }
52                 exit($query); /** 抛出出错的 sql 语句 */
53             }
54         }
55         if($isCommit){
56             return $this;
57         }else{
58             return $res;
59         }
60     }
61 
62     /** 开始事务 */
63     public function SWBegin(){
64         mysql_query("BEGIN",$this->link);
65     }
66 
67     /** 回滚 */
68     public function rollBack(){
69         mysql_query("ROLLBACK",$this->link);
70     }
71 
72     /** 提交事务 */
73     public function commit($getThis=false){
74         mysql_query("COMMIT",$this->link);
75         if($getThis){
76             return $this;
77         }else{
78             return null;
79         }
80     }
81 }
82 
83 ?>

5,例子

 1 <?php
 2 /**
 3  * Created by PhpStorm.
 4  * User: 林冠宏
 5  * Date: 2015/10/24
 6  * Time: 11:23
 7  */
 8 
 9 include "Sql.php";
10 $sql = new Sql();
11 
12 /** 往 aa 表中插入一批数据,注意建aa表的时候要选择 innoDB 引擎 */
13 for($i=0;$i<10;$i++){
14     $temp = $i."k";
15     $query = "insert into aa (a,w) values('$i','$temp')";
16     $sql->exec($query);
17 }
18 
19 /** 下面的注释君请自行开启看效果 */
20 //$sql->rollBack(); /** 回滚的话,上面的所有insert不会被执行 */
21 //$sql->commit();     /** 不commit提交的话,上面的insert也不会被执行 */
22 
23 /** select 操作不用commit也可以直接使用 结果集 */
24 /*
25 $result = $sql->exec("select * from aa");
26 while($row = mysql_fetch_assoc($result)){
27     print($row)."</br>";
28 }
29 */
30 
31 ?>
View Code

 

 

posted @ 2016-04-29 18:15  指尖下的幽灵  阅读(881)  评论(0编辑  收藏  举报