zoukankan      html  css  js  c++  java
  • 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();
        }
        
    }
  • 相关阅读:
    WEB安全第二篇--用文件搞定服务器:任意文件上传、文件包含与任意目录文件遍历
    WEB安全第一篇--对服务器的致命一击:代码与命令注入
    python的内存管理与垃圾回收机制学习
    java反序列化漏洞的检测
    python epoll实现异步socket
    Python class的属性访问控制和内建函数重写实现高级功能以及@property
    weblogic新漏洞学习cve-2017-10271
    PHP后门的eval类和system类 函数到底有哪些区别
    JS 转整型
    .NET MVC model数据验证
  • 原文地址:https://www.cnblogs.com/liangdong/p/10408623.html
Copyright © 2011-2022 走看看