PDO单例模式代码展示

<?php 
class MyPDO
{
    private static $instance;  //保存对象
    private $host;      //主机地址
    private $dbname;    //数据库名字
    private $port;      //端口
    private $user;      //用户名
    private $pwd;       //密码
    private $charset;   //字符集
    private $link;      //连接对象


    private function __construct($data){

        $this->initParam($data);
        $this->getPDO();
        $this->errorMode();
    }

    private function __clone(){

    }

    //获取单例
    public static function getInstance($data=array()){
        if(!self::$instance instanceof self){
            return self::$instance=new self($data);
        }
        return self::$instance;
    }

    //初始化参数
    private function initParam($data){
        $this->host=$data['host'] ?? 'localhost';
        $this->dbname=$data['dbname'] ?? 'my_db';
        $this->port=$data['port'] ?? '3306';
        $this->user=$data['user'] ?? 'root';
        $this->pwd=$data['host'] ?? 'root';
        $this->charset=$data['charset'] ?? 'utf8';
    }

    //显示错误
    private function showError($e,$sql=null){
        echo "错误信息".$e->getMessage()."<br>";
        echo "错误代码".$e->getCode()."<br>";
        echo "错误文件".$e->getFile()."<br>";
        echo "错误行号".$e->getLine().'<br>';
        if($sql!=null){
            echo "错误sql语句".$sql;
        }
    }
    //连接数据库
    private function getPDO(){
        try {
            $this->link= new PDO("mysql:host={$this->host};port={$this->port};dbname={$this->dbname};charset={$this->charset}","{$this->user}","{$this->pwd}");
        } catch (PDOException $e) {
           $this->showError($e);
            exit;
        }
    }

    //设置错误模式
    private function errorMode(){
        $this->link->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    }

    

    /**
     * 增删改功能
     * @param string sql语句
     * @return int 受影响的行数
     */
    public function exec($sql){
        try {
            return $row=$this->link->exec($sql);
        } catch (PDOException $e) {
            $this->showError($e,$sql);
            exit;
        }

    }

    /**
     * 获取二维数组结果集
     * @param string sql
     * @return PDOStatement 结果集
     */

     public function fetchAll($sql){
         try{
            $stmt=$this->link->query($sql);
            return $stmt->fetchAll();

         }catch(PDOException $e){
            $this->showError($e,$sql);
         }

     }

     /**
      * 获取一维数组结果集
      *@param string sql
      *@return PDOStatement 结果集
      */
      public function fetch($sql){
          try {
            $stmt= $this->link->query($sql);
            return $stmt->fetch();
          } catch (PDOException $e) {
              $this->showError($e,$sql);
          }

      }

      /**
       * 获取单行单列
       * @param string sql
       * @return mixed 内容
       */
      public  function fetchColumn($sql){
          try {
            $stmt=$this->link->query($sql);
            return $stmt->fetchColumn();
          } catch (PDOException $e) {
              $this->showError($e,$sql);
          }
      }

}


$pdo=MyPDO::getInstance();
$arr=$pdo->fetchColumn("select count(*) from my_bank");
print_r($arr);

 

posted @ 2021-01-22 11:48  WhiteSpace  阅读(72)  评论(0编辑  收藏  举报