一个关于php使用pdo方式进行数据库连接和处理的类

来源:互联网 发布:复杂网络的小世界特性 编辑:程序博客网 时间:2024/05/11 15:35
  1. <?php   
  2. /** 
  3.     @DB Operates For PDO 
  4.     @author:MeeeeN 
  5.     @date:2015-10-22 22:40:32 
  6. **/  
  7.   
  8.     //定义数据库信息  
  9.       
  10.     header("Content-type:text/html; charset=utf-8");  
  11.   
  12.     define('DB_HOST''localhost');  
  13.     define('DB_USER''root');  
  14.     define('DB_PWD''');  
  15.     define('DB_NAME''lesson');  
  16.   
  17.     class DBPDO {  
  18.   
  19.         private static $instance;         
  20.         public $dsn;         
  21.         public $dbuser;         
  22.         public $dbpwd;         
  23.         public $sth;         
  24.         public $dbh;   
  25.   
  26.         //初始化  
  27.         function __construct() {  
  28.             $this->dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME;  
  29.             $this->dbuser = DB_USER;  
  30.             $this->dbpwd = DB_PWD;  
  31.             $this->connect();  
  32.             $this->dbh->query("SET NAMES 'UTF8'");  
  33.             $this->dbh->query("SET TIME_ZONE = '+8:00'");  
  34.         }  
  35.   
  36.         //连接数据库  
  37.         public function connect() {  
  38.             try {  
  39.                 $this->dbh = new PDO($this->dsn, $this->dbuser, $this->dbpwd);  
  40.             }  
  41.             catch(PDOException $e) {  
  42.                 exit('连接失败:'.$e->getMessage());  
  43.             }  
  44.         }  
  45.   
  46.         //获取表字段  
  47.         public function getFields($table='vista_order') {  
  48.             $this->sth = $this->dbh->query("DESCRIBE $table");  
  49.             $this->getPDOError();  
  50.             $this->sth->setFetchMode(PDO::FETCH_ASSOC);  
  51.             $result = $this->sth->fetchAll();  
  52.             $this->sth = null;  
  53.             return $result;  
  54.         }  
  55.   
  56.         //插入数据  
  57.         public function insert($sql) {  
  58.             if($this->dbh->exec($sql)) {  
  59.                 $this->getPDOError();  
  60.                 return $this->dbh->lastInsertId();  
  61.             }  
  62.             return false;  
  63.         }  
  64.   
  65.         //删除数据  
  66.         public function delete($sql) {  
  67.             if(($rows = $this->dbh->exec($sql)) > 0) {  
  68.                 $this->getPDOError();  
  69.                 return $rows;  
  70.             }  
  71.             else {  
  72.                 return false;  
  73.             }  
  74.         }  
  75.   
  76.         //更改数据  
  77.         public function update($sql) {  
  78.             if(($rows = $this->dbh->exec($sql)) > 0) {  
  79.                 $this->getPDOError();  
  80.                 return $rows;  
  81.             }  
  82.             return false;  
  83.         }  
  84.   
  85.         //获取数据  
  86.         public function select($sql) {  
  87.             $this->sth = $this->dbh->query($sql);  
  88.             $this->getPDOError();  
  89.             $this->sth->setFetchMode(PDO::FETCH_ASSOC);  
  90.             $result = $this->sth->fetchAll();  
  91.             $this->sth = null;  
  92.             return $result;  
  93.         }  
  94.   
  95.         //获取数目  
  96.         public function count($sql) {  
  97.             $count = $this->dbh->query($sql);  
  98.             $this->getPDOError();  
  99.             return $count->fetchColumn();  
  100.         }  
  101.   
  102.         //获取PDO错误信息  
  103.         private function getPDOError() {  
  104.             if($this->dbh->errorCode() != '00000') {  
  105.                 $error = $this->dbh->errorInfo();  
  106.                 exit($error[2]);  
  107.             }  
  108.         }  
  109.   
  110.         //关闭连接  
  111.         public function __destruct() {  
  112.             $this->dbh = null;  
  113.         }  
  114.     }  
  115.   
  116.     //eg: an example for operate select  
  117.   
  118.     $test = new DBPDO;  
  119.   
  120.     $sql = "SELECT * FROM `vista_order` WHERE `id`!=100 ";  
  121.   
  122.     $rs = $test->select($sql);  
  123.   
  124.     print_r($rs);  
  125.       
  126.   
  127.   
  128.   
  129.   
  130. ?>  

这是之前研究了一段时间pdo后所写出来的一个pdo数据库相关操作类(比较懒,一直没更新博客),参考了一些网上的相关文章,但是感觉很多要么写得有错误,要么很啰嗦,所以自己搞了个,其实本来我是一直是用MySQL类连接的,但是升级了PHP版本后发现不支持mysql方式连接了,又感觉mysqli比较啰嗦,所以索性改为用pdo,其实基本功能来说的话,这个类中construct,connection,destruct三个function就足够了,不过方便快速使用的话还是多写了一些function,个人感觉这个类的可移植性还是蛮高的,最后有使用的例子,基本上引用DBPDO类之后,只要自己写好sql语句,增删改查就都可以实现了

来源:http://blog.csdn.net/meeeen7/article/details/52136474


原创粉丝点击