码迷,mamicode.com
首页 > 其他好文 > 详细

PDO 封装类

时间:2017-07-15 11:23:07      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:new   property   token   next   tin   opera   struct   second   sso   

<?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"));?>

PDO 封装类

标签:new   property   token   next   tin   opera   struct   second   sso   

原文地址:http://www.cnblogs.com/lovehx/p/7181870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!