PDO 封装类

<?php

/**
 * PDO Mysql 简单封装
 * @author Kl_PENG QQ群:207776690
 */
class PdoHelper {

    //单例模式
    private static $_instance;

    const DB_HOST = 'localhost';
    const DB_PORT = '3306';
    const DB_NAME = 'test';
    const DB_USER = 'root';
    const DB_PASS = '';
    const DB_CHARSET = 'utf8';

    protected $_sql = '';

    //构造函数
    private function __construct() {
        $this->dsn = 'mysql:host=' . self::DB_HOST . ';port=' . self::DB_PORT . ';dbname=' . self::DB_NAME;
        $this->dbuser = self::DB_USER;
        $this->dbpass = self::DB_PASS;
        $this->connect();
        $this->pdo->query('SET NAMES ' . self::DB_CHARSET);
    }
    
    //析构函数
    public function __destruct() {
                $this->pdo = null;
        }

    //禁止克隆
    public function __clone() {
        
    }

    //单例对象
    public static function getInstance() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    //连接数据库       
    private function connect() {
        try {
            $this->pdo = new PDO($this->dsn,$this->dbuser,$this->dbpass);}catch(PDOException$e){exit('连接失败:'.$e->getMessage());}}//返回最后一次执行的sqlpublicfunctiongetLastSql(){return$this->_sql;}//查询全部记录publicfunctiongetAll($sql=""){$obj=$this->pdo->query($sql);$this->_sql=$sql;$result=$obj->fetchAll(constant('PDO::FETCH_ASSOC'));return$result;}//查询一条记录publicfunctiongetRow($sql=""){$obj=$this->pdo->query($sql);$this->_sql=$sql;$result=$obj->fetch(constant('PDO::FETCH_ASSOC'),constant('PDO::FETCH_ORI_NEXT'));return$result;}//执行sqlpublicfunctionexecSql($sql=""){$result=$this->pdo->exec($sql);$this->_sql=$sql;return$result;}//根据id查询记录publicfunctiongetById($table,$id,$fields='*'){$sql="SELECT ".$fields." FROM ".$table." WHERE id=".$id;returnself::getRow($sql);}/**
     * 查询N个字段的值
     * @param string $table 表名
     * @param string $where 参数示例: "id = 1"
     * @param string $fields 参数示例: "id,val,..."
     */publicfunctiongetFields($table,$where,$fields='*'){$sql="SELECT ".$fields." FROM ".$table." WHERE ".$where;returnself::getRow($sql);}/**
     * 添加一条记录
     * @param string $table 表名
     * @param string $data 一维数组 示例: array('id'=>1,'val'=>2)
     */publicfunctionadd($table,$data){if(!is_array($data)||!$data){return"Error: The second parameter must be an array";}$sql="INSERT INTO ".$table." (";$fields=implode(",",array_keys($data));$values=implode(",",array_values($data));$sql.=$fields.") VALUES (".$values.")";returnself::execSql($sql);}/**
     * 添加多条记录
     * @param string $table 表名
     * @param string $data 二维数组 
     * @param $data 参数示例: array(array('id'=>1,'val'=>2),array('id'=>2,'val'=>3))
     */publicfunctionaddAll($table,$data){if(!is_array($data)||!$data){return"Error: The second parameter must be an array";}$sql="INSERT INTO ".$table." (";$fields=implode(",",array_keys($data[0]));$values="";foreach($dataas$d){$values.=implode(",",array_values($d))."),(";}$values=rtrim($values,"),(");$sql.=$fields.") VALUES (".$values.")";returnself::execSql($sql);}/**
     * 更新记录
     * @param string $table 表名
     * @param string $set 参数示例:"field = val"
     * @param string $where 参数示例: "id = 1"
     */publicfunctionupdate($table,$set,$where){$sql="UPDATE ".$table." SET ".$set." WHERE ".$where;returnself::execSql($sql);}/**
     * 删除记录
     * @param string $table 表名
     * @param string $where 参数示例: "id = 1"
     */publicfunctiondel($table,$where){$sql="DELETE FROM ".$table." WHERE ".$where;returnself::execSql($sql);}}//调用示例$k=PdoHelper::getInstance();print_r($k->getAll("select * from table_test"));?>
原文地址:https://www.cnblogs.com/lovehx/p/7181870.html