个人封装PHP Mysql PDO操作类(单例)

Original
2020-12-29 / 0 评论 / 5 阅读 / 正在检测是否收录...

pdoHelper.class.php

防SQL注入
单例对象

<?php
    class pdoHelper{
        protected $host;
        protected $port;
        protected $dbUserName;
        protected $dbUserPwd;
        protected $dbName;
        protected $charset;
        protected $dbType;
        protected $isPconnect = true;
        protected $pdo;
        protected static $_pdoInstance = null;

        public static function getPdoInstance($conf=[]){
            if(!self::$_pdoInstance instanceof self){
                self::$_pdoInstance = new self($conf);
            }
            return self::$_pdoInstance;
        }

        private function __construct($conf){
            class_exists('PDO') or die('PDO: class not exists');
            $this->init($conf);

            if(!self::$_pdoInstance){
                $this->connect();
            }
        }

        private function init($conf=[]){
            $defaultConf = [
                'dbType'=> 'mysql',
                'host'=> 'localhost',
                'port'=> 3306,
                'dbUserName'=> 'root',
                'dbUserPwd'=> '123123',
                'dbName'=> 'test',
                'charset'=> 'utf8'
            ];

            foreach($defaultConf as $key => $val) {
                $this->$key = $conf[$key] ?: $defaultConf[$key];
            }
        }

        private function connect(){
            $dsn = "{$this->dbType}:host={$this->host};port={$this->port};dbname={$this->dbName};charset={$this->charset}";

            $pdoOptions = $this->isPconnect ? [PDO::ATTR_PERSISTENT=> true] : [];
            try{
                $this->pdo = new PDO($dsn, $this->dbUserName, $this->dbUserPwd, $pdoOptions);
                $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
            }catch(PDOException $e){
                $this->dealError($e);
            }
        }

        public function selectOne($sql, $params=[]){
            return $this->select($sql, $params);
        }

        public function selectAll($sql, $params=[]){
            return $this->select($sql, $params, true);
        }

        public function insert($sql, $params=[]){
            return $this->execute($sql, $params);
        }

        public function delete($sql, $params=[]){
            return $this->execute($sql, $params);
        }

        public function update($sql, $params=[]){
            return $this->execute($sql, $params);
        }

        private function select($sql, $params=[], $isAll=false){
            $func = $isAll ? 'fetchAll' : 'fetch';
            try{
                $pdoStmt = $this->pdo->prepare($sql);
                $pdoStmt->execute($params);
                $result = $pdoStmt->$func(PDO::FETCH_ASSOC);
            }catch(PDOException $e){
                $this->dealError($e);
            }

            return $result;
        }

        private function execute($sql, $params){
            try{
                $pdoStmt = $this->pdo->prepare($sql);
                $pdoStmt->execute($params);
                $result = $pdoStmt->rowCount(PDO::FETCH_ASSOC);
            }catch(PDOException $e){
                $this->dealError($e);
            }

            return $result;
        }

        private function __clone(){}

        private function dealError($e, $sql='null'){
            echo json_encode([
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'script' => $e->getFile(),
                'errorLine' => $e->getLine()
            ]);
        }
    }
    // example
    $mysql = pdoHelper::getPdoInstance(['dbName'=> 'answer']);
    $sql = "select * from register";
    $result = $mysql->selectOne($sql);
    echo json_encode($result);

console

PS F:\souceCode\website\user\class> php .\pdoHelper.class.php
{"Id":3,"username":"haha","userpwd":"mima","times":0,"nowTime":"2020-12-29 12:34:30","ip":"127.0.0.1","state":0,"info":""}
0

评论 (0)

取消