php MySQLDB类

文件名必须与类名一致

文件名为:MySQLDB.class.php

<?php
//连接数据库
//$db = new MySQLi('localhost:3306','root','','z_1031');
//!mysqli_connect_error() or die('数据库连接失败');
//$db->query('set names utf8');  //设置字符集
//
//
//$sql = "";
//$res = $db->query($sql);
//$arr = $res->fetch_all();




class MySQLDB{
    public $host;  //服务器地址
    public $name;  //数据库账号
    public $pwd;  //数据库密码
    public $dbname;  //数据库名称
    public $charset; //数据库字符编码
    public $post;  //数据库端口号
    public $db;  //mysqli对象
    //三私一公   单例 某个类对象的单例
    private static $MyDb;  //MySQLDB对象
    private function __clone(){}
    
    
    static function getMyDb($config = array()){
        if(!isset(MySQLDB::$MyDb)){
            MySQLDB::$MyDb = new self($config);
        }
        return MySQLDB::$MyDb;
    }
    
    
    //构造方法,主要用来初始化对象
    private function __construct($config){
        $this->host = isset($config['host']) ? $config['host']:'localhost'; //isset检测变量是否设置
        $this->name = isset($config['name']) ? $config['name']:'root';
        $this->pwd = isset($config['pwd']) ? $config['pwd']:'';
        $this->dbname = isset($config['dbname']) ? $config['dbname']:'z_1031';
        $this->charset = isset($config['charset']) ? $config['charset']:'utf8';
        
        
        
        //实例化MySQLi对象
        $this->getDb();
        //设置字符集
        $this->charset();
    }
    //实例化mysqli对象
    function getDb(){
        $this->db = new MySQLi($this->host,$this->name,$this->pwd,$this->dbname);
    }
    //设置字符集
    function charset(){
        $this->db->query('set names '.$this->charset);
    }
    //执行sql语句   增删改
    function query($sql){
        $res = $this->db->query($sql);
        if(!$res){
            echo ("<br />执行失败。");
            echo "<br />失败的sql语句为:" . $sql;
            echo "<br />出错信息为:" . mysqli_error($this->link);
            echo "<br />错误代号为:" . mysqli_errno($this->link);
            die;
        }
        return $res;
    }
    
    
    //返回字符串
    function getStr($sql){
        $res = $this->query($sql);
        $attr = $res->fetch_all();
        $str = '';
        foreach($attr as $v){
            $str .= implode(',', $v).'^';
        }
        $str = substr($str,0,-1);
        return $str;
    }
    
    //返回json
    function getJson($sql){
        /*  ↓这句相当于“ $res=$db->query($sql);” */
        $res=$this->query($sql);
        $arr=array();
        while($row=$res->fetch_assoc()){
//              ↓PHP往数组里追加元素
            $arr[]=$row;
        }
        //返回json数组
        return json_encode($arr);        
    }
    //返回关联数组
    function getAssoc($sql){
        /*  ↓这句相当于“ $res=$db->query($sql);” */
        $res=$this->query($sql);
        $arr=array();
        while($row=$res->fetch_assoc()){
//              ↓PHP往数组里追加元素
            $arr[]=$row;
        }
//        返回关系数组 此时 $arr 就是一个关系数组,直接返回即可
        return $arr;
    }
    
    //返回第一条
    function getOne($sql){
        $res=$this->query($sql);
        return $res->fetch_row();
    }
    
}

 

posted @ 2019-02-20 19:38  梁栋  阅读(389)  评论(0编辑  收藏  举报