PDO数据库初始化连接数据库文件代码(附源码下载)

[复制链接]
查看1184 | 回复0 | 2020-11-16 16:08 | 显示全部楼层 |阅读模式
PDO数据库初始化文件代码(附源码下载)
  1. <?php
  2. class MyPDO{
  3.     private $type;      //数据库类别
  4.     private $host;      //主机地址
  5.     private $port;      //端口号
  6.     private $dbname;    //数据库名
  7.     private $charset;   //字符集
  8.     private $user;      //用户名
  9.     private $pwd;       //密码
  10.     private $pdo;       //保存PDO对象
  11.     private static $instance;
  12.     private function __construct($param) {
  13.         $this->initParam($param);
  14.         $this->initPDO();
  15.         $this->initException();
  16.     }
  17.     private function __clone() {
  18.     }
  19.     public static function getInstance($param=array()){
  20.         if(!self::$instance instanceof self)
  21.             self::$instance=new self($param);
  22.         return self::$instance;
  23.     }
  24.     //初始化参数
  25.     private function initParam($param){
  26.         $this->type=$param['type']??'mysql';
  27.         $this->host=$param['host']??'127.0.0.1';
  28.         $this->port=$param['port']??'3306';
  29.         $this->dbname=$param['dbname']??'data';
  30.         $this->charset=$param['charset']??'utf8';
  31.         $this->user=$param['user']??'root';
  32.         $this->pwd=$param['pwd']??'root';
  33.     }
  34.     //初始化PDO
  35.     private function initPDO(){
  36.         try{
  37.             $dsn="{$this->type}:host={$this->host};port={$this->port};dbname={$this->dbname};charset={$this->charset}";
  38.             $this->pdo=new PDO($dsn, $this->user, $this->pwd);
  39.         } catch (PDOException $ex) {
  40.             $this->showException($ex);
  41.             exit;
  42.         }
  43.     }
  44.    
  45.     //显示异常
  46.     private function showException($ex,$sql=''){
  47.         if($sql!=''){
  48.             echo 'SQL语句执行失败<br>';
  49.             echo '错误的SQL语句是:'.$sql,'<br>';
  50.         }
  51.         echo '错误编号:'.$ex->getCode(),'<br>';
  52.         echo '错误行号:'.$ex->getLine(),'<br>';
  53.         echo '错误文件:'.$ex->getFile(),'<br>';
  54.         echo '错误信息:'.$ex->getMessage(),'<br>';
  55.     }
  56.     //设置异常模式
  57.     private function initException(){
  58.         $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  59.     }

  60.     //执行增、删、改操作
  61.     public function exec($sql){
  62.         try{
  63.             return $this->pdo->exec($sql);
  64.         } catch (PDOException $ex) {
  65.             $this->showException($ex, $sql);
  66.             exit;
  67.         }
  68.     }
  69.     //获取自动增长的编号
  70.     public function lastInsertId(){
  71.         return $this->pdo->lastInsertId();
  72.     }
  73.    
  74.     //判断匹配的类型
  75.     private function fetchType($type){
  76.         switch ($type){
  77.             case 'num':
  78.                 return PDO::FETCH_NUM;
  79.             case 'both':
  80.                 return PDO::FETCH_BOTH;
  81.             case 'obj':
  82.                 return PDO::FETCH_OBJ;
  83.             default:
  84.                  return PDO::FETCH_ASSOC;
  85.         }
  86.     }
  87.     //获取所有数据 ,返回二维数组
  88.     public function fetchAll($sql,$type='assoc'){
  89.         try{
  90.             $stmt=$this->pdo->query($sql);  //获取PDOStatement对象
  91.             $type= $this->fetchType($type); //获取匹配方法
  92.             return $stmt->fetchAll($type);
  93.         } catch (Exception $ex) {
  94.             $this->showException($ex, $sql);
  95.         }
  96.     }
  97.     //获取一维数组
  98.     public function fetchRow($sql,$type='assoc'){
  99.         try{
  100.             $stmt=$this->pdo->query($sql);  //获取PDOStatement对象
  101.             $type= $this->fetchType($type); //获取匹配方法
  102.             return $stmt->fetch($type);
  103.         } catch (Exception $ex) {
  104.             $this->showException($ex, $sql);
  105.             exit;
  106.         }
  107.     }
  108.     //返回一行一列
  109.     public function fetchColumn($sql){
  110.         try{
  111.              $stmt=$this->pdo->query($sql);
  112.             return $stmt->fetchColumn();
  113.         } catch (Exception $ex) {
  114.             $this->showException($ex, $sql);
  115.             exit;
  116.         }
  117.         
  118.     }
  119.    
  120. }

复制代码
源码下载:
MyPDO.class.zip (1.35 KB, 下载次数: 568)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-4-16