zoukankan      html  css  js  c++  java
  • PHP单例模式封装 MySQL-PDO

    <?php
    class MyPDO{
        private $type;      //数据库类别
        private $host;      //主机地址
        private $port;      //端口号
        private $dbname;    //数据库名
        private $charset;   //字符集
        private $user;      //用户名
        private $pwd;       //密码
        private $pdo;       //保存PDO对象
        private static $instance;
        private function __construct($param) {
            $this->initParam($param);
            $this->initPDO();
            $this->initException();
        }
        private function __clone() {
        }
        public static function getInstance($param=array()){
            if(!self::$instance instanceof self)
                self::$instance=new self($param);
            return self::$instance;
        }
        //初始化参数
        private function initParam($param){
            $this->type=$param['type']??'mysql';
            $this->host=$param['host']??'127.0.0.1';
            $this->port=$param['port']??'3306';
            $this->dbname=$param['dbname']??'sel';
            $this->charset=$param['charset']??'utf8';
            $this->user=$param['user']??'root';
            $this->pwd=$param['pwd']??'';
        }
        //初始化PDO
        private function initPDO(){
            try{
                $dsn="{$this->type}:host={$this->host};port={$this->port};dbname={$this->dbname};charset={$this->charset}";
                $this->pdo=new PDO($dsn, $this->user, $this->pwd);
            } catch (PDOException $ex) {
                $this->showException($ex);
                exit;
            }
        }
        
        //显示异常
        private function showException($ex,$sql=''){
            if($sql!=''){
                echo 'SQL语句执行失败<br>';
                echo '错误的SQL语句是:'.$sql,'<br>';
            }
            echo '错误编号:'.$ex->getCode(),'<br>';
            echo '错误行号:'.$ex->getLine(),'<br>';
            echo '错误文件:'.$ex->getFile(),'<br>';
            echo '错误信息:'.$ex->getMessage(),'<br>';
        }
        //设置异常模式
        private function initException(){
            $this->pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
        }
    
        //执行增、删、改操作
        public function exec($sql){
            try{
                return $this->pdo->exec($sql);
            } catch (PDOException $ex) {
                $this->showException($ex, $sql);
                exit;
            }
        }
        //获取自动增长的编号
        public function lastInsertId(){
            return $this->pdo->lastInsertId();
        }
    
        //判断匹配的类型
        private function fetchType($type){
            switch ($type){
                case 'num':
                    return PDO::FETCH_NUM;
                case 'both':
                    return PDO::FETCH_BOTH;
                case 'obj':
                    return PDO::FETCH_OBJ;
                default:
                     return PDO::FETCH_ASSOC;
            }
        }
        //获取所有数据 ,返回二维数组
        public function fetchAll($sql,$type='assoc'){
            try{
                $stmt=$this->pdo->query($sql);  //获取PDOStatement对象
                $type= $this->fetchType($type); //获取匹配方法
                return $stmt->fetchAll($type);
            } catch (Exception $ex) {
                $this->showException($ex, $sql);
            }
        }
        //获取一维数组
        public function fetchRow($sql,$type='assoc'){
            try{
                $stmt=$this->pdo->query($sql);  //获取PDOStatement对象
                $type= $this->fetchType($type); //获取匹配方法
                return $stmt->fetch($type);
            } catch (Exception $ex) {
                $this->showException($ex, $sql);
                exit;
            }
        }
        //返回一行一列
        public function fetchColumn($sql){
            try{
                 $stmt=$this->pdo->query($sql);
                return $stmt->fetchColumn();
            } catch (Exception $ex) {
                $this->showException($ex, $sql);
                exit;
            }
        }
    }
    
    //测试
    $param=array(
       
    );
    $mypdo= MyPDO::getInstance($param);
    
    if($mypdo->exec("insert into news values (null,'11','1111',unix_timestamp())"))
        echo '自动增长的编号是:'.$mypdo->lastInsertId ();
    
    $list=$mypdo->fetchColumn('select count(*) from news');
    var_dump($list);
    ?>
  • 相关阅读:
    Python004-数据处理示例:以某个数据(字段)为基准从数据中获取不同的字段行数
    VUE-004-禁止修改页面显示项,设置el-input,textarea只读方法
    VUE-003-前端表格数据展示时,设置单元格(el-table-column)保留空格和换行
    cobaltstrike4.0 cracked
    CobaltStrike3.14破解
    CobaltStrike3.12/13 破解
    Windows 10 Manager v2.3.3
    MobaXterm v10.9破解
    Jupyter/JupyterLab安装使用
    WordPress <= 4.6 命令执行漏洞(PHPMailer)复现分析
  • 原文地址:https://www.cnblogs.com/SharkJiao/p/14137542.html
Copyright © 2011-2022 走看看