一个PDO类

下面是在网上借鉴的一个PDO类:

  1 <?php
  2 class Database{
  3     private $host = DB_HOST;
  4     private $user = DB_USER;
  5     private $pass = DB_PASS;
  6     private $dbname = DB_NAME;
  7 
  8     private $dbh;
  9     private $error;
 10 
 11     private $stmt;
 12 
 13         /*
 14         function __construct 
 15         */
 16     public function __construct(){
 17         // set DSN
 18         $dsn = 'mysql:host='.$this->host.';dbname='.$this->dbname;
 19         //set options
 20         $options = array(
 21             PDO::ATTR_PERSISTENT => true,
 22             PDO::ATTR_ERRMODE    => PDO::ERRMODE_EXCEPTION    
 23         );
 24         //create a new PDO instanace
 25         try{
 26             $this->dbh = new PDO($dsn,$this->user,$this->pass,$options);
 27         }
 28         //catch any errors
 29         catch(PDOException $e){
 30             $this->error = $e->getMessage();
 31         }
 32     }
 33 
 34         /*
 35         function query
 36         */
 37     public function query($query){
 38         $this->stmt = $this->dbh->prepare($query);
 39     }    
 40 
 41         /*
 42           fucntion bindValue
 43         */
 44     public function bind($param,$value,$type){
 45         if (is_null($type)) {
 46         switch (true) {
 47             case is_int($value):
 48                 $type = PDO::PARAM_INT;
 49                 break;
 50             case is_bool($value):
 51                 $type = PDO::PARAM_BOOL;
 52                 break;
 53             case is_null($value):
 54                 $type = PDO::PARAM_NULL;
 55                 break;
 56             default:
 57                 $type = PDO::PARAM_STR;
 58         }
 59     }
 60     $this->stmt->bindValue($param, $value, $type);
 61     }    
 62 
 63     /*
 64       functin execute
 65     */  
 66     public function execute(){
 67         return $this->stmt->execute();
 68     }
 69     /*
 70     获得结果集  关联数组
 71     */
 72     public function resultset(){
 73     $this->execute();
 74     return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
 75     }
 76 
 77 
 78     /* 获得单条数据*/
 79     public function single(){
 80         $this->execute();
 81         return $this->stmt->fetch(PDO::FETCH_ASSOC);
 82     }
 83     
 84     public function rowCount(){
 85             return $this->stmt->rowCount();
 86     }
 87 
 88     public function lastInsertId(){
 89         return $this->dbh->lastInsertId();
 90     }
 91 
 92     public function beginTransaction(){
 93         return $this->dbh->beginTransaction();
 94     }
 95 
 96     public function endTransaction(){
 97         return $this->dbh->commit();
 98     }
 99 
100     public function cancelTransaction(){
101            return $this->dbh->rollBack();
102     }
103 
104     public function debugDumpParams(){
105         return $this->stmt->debugDumpParams();
106     }
107 
108 }
109 ?>

这个类的具体使用:

 try{
      $database = new Database();
      $database->beginTransaction();
      $tm_startt = strtotime($_POST['time_start']);
      $tm_end = strtotime($_POST['time_end']);
      $database->query('insert into gua_user(user_name, user_nicheng, user_type, user_comment,user_created,user_end) values (:user_name, :user_nicheng, :user_type, :user_comment,:user_created,:user_end)');
      $database->bind(':user_name', $_POST['username']);
      $database->bind(':user_nicheng', $_POST['user_nick']);
      $database->bind(':user_type', $_POST['user_type']);
      $database->bind(':user_comment', $_POST['comment']);
      $database->bind(':user_created', $tm_startt);
      $database->bind(':user_end', $tm_end);
      $database->execute();
      //获得影响的行数
      $rows = $database->rowCount();
      //获得本条数据的id
      $id = $database->lastInsertId();
      if($rows<1){
          throw new PDOexception('第一句sql语句执行失败!', '01');
      }
      //产生随机字符串 salt
      $salt = $database->salt();
      $password = md5($_POST['password'].$salt);
      $database->query('insert into gua_salt(uid,salt,password) values (:uid,:salt,:password)');
      $database->bind(':uid',$id);
      $database->bind(':salt',$salt);
      $database->bind(':password',$password);
      $database->execute();
      $rows = $database->rowCount();
      if($rows<1){
          throw new PDOexception('第二句sql语句执行失败!', '02');
      }
      $database->endTransaction();
      }catch(PDOexception $e){
          //如果有异常被抛出 则事务失败 执行事务回滚
          $database->cancelTransaction();
          //输出异常信息
          echo $e->getCode().'-----'.$e->getMessage();
      }

使用事物,注意表的类型要是innodb的。

参考网站:http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/

posted @ 2014-04-20 20:37  lizcao  阅读(273)  评论(0编辑  收藏  举报