• 封装好的MySQL.class.php类


    封装好的MySQL.class.php类

    作用:数据库操作类

    <?php
    header('content-type:text/html;charset=utf-8');
    class MySQLDB{
        private $host;   //主机地址
        private $port;     //端口号
        private $user;     //用户名
        private $pass;     //密码
        private $dbname; //数据库名
        private $charset;//字符集
        private $link;      //保存连接数据库资源
        private static $instance = null;     //单例模式中的$MySQLDB对象
    
        /**
        *@param array $config 配置文件
        */
        private function __construct ($config) {
        
            // 初始化属性的值
            $this->init($config);
            // 连接数据库
            $this->my_connect();
            // 设置字符集
            $this->my_charset();
            // 选择数据库
            $this->my_dbname();
        }
    
    
        /**
        * 只能new一次,单例模式
        *@param array $config 传递给构造方法的数组参数
        */
        public static function getInstacne($config) {
            if (self::$instance == null) {
                self::$instance = new self($config);
            }
            return self::$instance;
        }
    
        /***
        *私有化克隆魔术方法
        */
        private function _clone() {}
    
        /**
        *初始化
        *@param array $config 传递给初始化方法的数组参数
        */
        private function init($config) {
            $this->host = isset($config['host'])?$config['host']:'localhost';
            $this->port = isset($config['port'])?$config['port']:'3306';
            $this->user = isset($config['user'])?$config['user']:'root';
            $this->pass = isset($config['pass'])?$config['pass']:'';
            $this->dbname = isset($config['dbname'])?$config['dbname']:'';
            $this->charset = isset($config['charset'])?$config['charset']:'utf8';
        }
    
        /**
        *错误测试方法
        *@param string $sql sql语句
        *直接用途:用来增删改
        */
        public function my_query($sql) {
            if ($result = mysql_query($sql)) {
                return $result;
            }else {
                echo "执行sql语句失败;<br/>";
                echo "错误代码:",mysql_errno(),"<br/>";
                echo "错误信息:",mysql_error(),'<br/>';
                return false;
            }
        }
        /**
        *连接数据库
        */
        private function my_connect() {
            if ($link = mysql_connect("$this->host:$this->port",$this->user,$this->pass)) {
                $this->link = $link;
            }else {
                echo "连接数据库失败:<br/>";
                echo "错误代码:",mysql_errno(),"<br/>";
                echo "错误信息:",mysql_error(),'<br/>';
                return false;
            }
        }    
    
        /**
        *设置字符集
        */
        private function my_charset() {
            $sql = "set names $this->charset";
            $this->my_query($sql);
        }
        /**
        *选择数据库
        */
        private function my_dbname() {
            $sql = "use $this->dbname";
            return $this->my_query($sql);
        }
    
    
    
        /** 
         *返回多行多列的结果集,二维数组
         *@param string sql语句
         *@return mixed(array|false) *执行成功返回数组,失败返回false
         */
        public function fetchAll($sql) {
            if($result = $this->my_query($sql)) {
                //返回资源结果集,遍历
                $rows[] = '';
                while ($row = mysql_fetch_assoc($result)) {
                    $rows[] = $row;
                }
                //结果集使用完毕,主动释放
                mysql_free_result($result);
                //返回二维数组
                return $rows;
            } else {
                return false;
            }
            
        }
     
    
        /**
        * 返回一行多列的结果集,一维数组
        *@param string 一条sql语句
        *@return mixed(array|false) 执行成功返回数组,失败返回false
         */
        public function fetchRow($sql) {
            if($result = $this->my_query($sql)) {
                //返回资源结果集,遍历
                $row = mysql_fetch_assoc($result);
                //结果集使用完毕,主动释放
                mysql_free_result($result);
                //返回一维数组
                return $row;
            } else {
                return false;
            }
        }
    
        /**
         *返回一行一列的结果集,单一值
         *@param string 一条sql语句
         *@return mixed(array|false) 执行成功返回数组,失败返回false
         */
        public function fetchColumn($sql) {
            if($result = $this->my_query($sql)) {
                //返回资源结果集,提取
                $row = mysql_fetch_row($result);
                //结果集使用完毕,主动释放
                mysql_free_result($result);
                //返回单一值
                return isset($row[0])?$row[0]:false;
            } else {
                return false;
            }
        }
        
        
    
        /**
        *析构方法
        */
        public function __destruct() {
            @mysql_close($this->link); 
        }
        /**
        *__sleep 序列化对象的时候触发执行
        */
        public function __sleep() {
            return array('host','port','user','pass','charset','dbname') ;
        }
    
    
        /**
        *__wakeup 反序列化的时候触发执行
        *初始化操作
        */
    
        public function __wakeup() {
             //初始化操作
             // 连接数据库
             $this->my_connect();
             // 设置字符集
             $this->my_charset();
             // 选择数据库
             $this->my_dbname();
         }
    
    
        /**
        *__set 为一个不可访问的属性赋值的时候自动触发
        *@param string $name 属性名
        *@param mixed $value 属性值
        */
         public function __set($name,$value) {
             $allow_set = array('host','port','user','pass','dbname','charset');
             if(in_array($name,$allow_set)) {
                 //当前属性可以被赋值
                 $this->$name = $value;
             }
         }
    
        /**
        *__get *获得一个不可访问的属性的值的时候自动触发
        *@param string $name 属性名
        */
         public function __get($name) {
             $allow_get = array('host','port','user','pass','dbname','charset');
             if (in_array($name,$allow_get)) {
                 return $this->$name;
             }
         }
    
        /**
        *__call 访问一个不可访问的对象方法的时候触发
        *@param string $name
        *@param array $argument 参数列表
        */
         public function __call($name, $argument) {
             echo "对不起,您访问的".$name."()方法不存在!<br/>";
         }
    
        /**
        *__callstatic 访问一个不可访问的类方法(静态方法)的时候触发
        *@param string $name
        *@param array $argument 参数列表
         */
         public static function __callStatic($name, $argument) {
             echo "对不起,您访问的".$name."()静态方法不存在!<br/>";
         }
    
         /**
         *调用初始化方法
         *@param array $config 配置文件数组
         */
         public function  get_init($config) {
             $this->init($config);
         }
    
         /** 调用连接数据库方法*/
         public function get_my_connect() {
             $this->my_connect();
         }
    
         /** 调用设置字符集方法*/
         public function get_my_charset() {
             $this->my_charset();
         }
    
         /** 调用选择数据库方法*/
         public function get_my_dbname() {
             $this->my_dbname();
         }
    
    
    
    }    

    调用例子:

    include './MySQLDB.class.php';
    $config = [
        'pass'=>'123456',
        'dbname'=>'bbs'
    ];
    $db = MySQLDB::getInstacne($config);
    $db::show();
    $db->hahaha();
    
    echo "<hr/>";
  • 相关阅读:
    Debian8搭建LEMP环境
    ProjectManager Beta 7 项目管理器发布
    我的Linux软件集
    修改/home内子目录的名字
    Nginx配置特定二级域名
    Debian8 安装wordpress博客
    LinuxMint18使用单独分区作为Home挂载点
    LinuxMint18配置Grub2默认启动操作系统
    《失恋33天》从绝境中走出来的故事
    爱的世界很拥挤,写在读《爱,就这么简单》之后
  • 原文地址:https://www.cnblogs.com/mrszhou/p/7581926.html
Copyright © 2020-2023  润新知