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

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

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

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



本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部