封装一个PDO_MySQL工具类

PDO_MySQL工具类

功能:

  1. 单例创建工具类对象,并连接MySQL
  2. 对mysql_query做封装,错误时打印错误SQL语句、出错信息、出错码
  3. 查询方法1:返回查询的全部结果,并作为数组返回
  4. 查询方法2:返回第一行数据,并作为数据返回
  5. 查询方法3:返回第一行的第一列数据,并作为数据返回
_initParams($config);//初始化配置参数
        $this->_initDSN();//初始化DSN
        $this->_initDriverOptions();//初始化驱动选项
        $this->_initPDO();//初始化pdo对象
    }
    private function __clone() {

    }
    public static function getInstance($config) {
        if (! static::$_instance instanceof static) {
            static::$_instance = new static($config);
        }
        return static::$_instance;
    }

    //初始化配置参数
    private function _initParams($config) {
        //初始化数据
        $this->_host = isset($config['host']) ? $config['host'] : 'localhost';
        $this->_port = isset($config['port']) ? $config['port'] : '3306';
        $this->_username = isset($config['username']) ? $config['username'] : 'root';
        $this->_password = isset($config['password']) ? $config['password'] : '';
        $this->_charset = isset($config['charset']) ? $config['charset'] : 'utf8';
        $this->_dbname = isset($config['dbname']) ? $config['dbname'] : '';
    }
    //初始化DSN
    private function _initDSN() {
        $this->_dsn = "mysql:host=$this->_host;port=$this->_port;dbname=$this->_dbname";
    }
    //初始化驱动选项
    private function _initDriverOptions() {
        $this->_driver_options = array(
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES $this->_charset",
            );
    }
    //初始化pdo对象
    private function _initPDO() {
        $this->_pdo = new PDO($this->_dsn, $this->_username, $this->_password, $this->_driver_options);
    }

    //执行SQL语句
    public function query($sql) {
        if(! $result = $this->_pdo->query($sql))
        {
            $error_info = $this->_pdo->errorInfo();
            echo "
执行失败。"; echo "
失败的sql语句为:" . $sql; echo "
出错信息为:" . $error_info[2]; echo "
错误代号为:" . $error_info[1]; die; } return $result; } //获取全部数据 public function getAll($sql) { //执行 $result = $this->query($sql); //获取数据,关联 $list = $result->fetchAll(PDO::FETCH_ASSOC); //关闭光标(释放) $result->closeCursor(); return $list; } //获取一行数据 public function getRow($sql) { $result = $this->query($sql); $row = $result->fetch(PDO::FETCH_ASSOC); $result->closeCursor(); return $row; } //获取一个数据 public function getOne($sql) { $result = $this->query($sql); $string = $result->fetchColumn(); $result->closeCursor(); return $string; } public function escapeString($data) { return $this->_pdo->quote($data); } }

你可能感兴趣的:(封装一个PDO_MySQL工具类)