• 定义一个连接数据库的类



    //连接数据库
    class dbda{
      //定义数据库连接定义参数
      public $host = 'localhost'; //一般都是localhost
      public $user = 'root';   //访问数据库时的身份

      public $pwd = '';  //数据库密码
      public $database = 'gps';  //所使用的数据库的名字
      public $charset = 'utf8';  //这是字符集

      public $db = null;     //数据库连接对象
      public $res = null;    //结果集

      //初始化数据
      function __construct($configArr = array()){

        //通过三元运算符进行比较
        $this ->host = isset($configArr['host']) ? $configArr['host']:'localhost';
        $this ->user = isset($configArr['user']) ? $configArr['user']:'root';
        $this ->pwd = isset($configArr['pwd']) ? $configArr['pwd']:'';
        $this ->database = isset($configArr['database']) ? $configArr['database']:'gps';
        $this ->charset = isset($configArr['charset']) ? $configArr['charset']:'utf8';

        //连接数据库
      $this -> connect();
    }
    //连接数据库
      function connect(){
        $this -> db = new MySQLi($this->host,$this->user,$this->pwd,$this->database);
        !mysqli_connect_error() or die ('连接失败');
        $this->db->query('set names utf8');
    }
      //执行sql语句的方法
      function query($sql){
        $res = $this->db->query($sql);
        if(!$res){
          echo ("<br />执行失败。");
          echo "<br />失败的sql语句为:" . $sql;
          echo "<br />出错信息为:" . mysql_error($this->db);
          echo "<br />错误代号为:" . mysql_errno($this->db);
        die();
        }
        return $res;
      }
      //返回结果集转成二维数组
       function getAll($sql){
        $res = $this->query($sql);
        return $res -> fetch_all();
      }
    //返回字符串

    //返回josn

    //返回关联数组
      function getAssoc($sql){
        $result = $this->query($sql);
        $arr=array();
        while($row = $result->fetch_assoc()){
          $arr[$row['uid']] = $row;
          }
          return $arr;
        }
      }

    //实例化

      $aaaa = new dbda();
      $sql ='select * from login';
      $att =$aaaa->getAssoc($sql);
      var_dump($att);

    php引用

    function __autoload($className){
        require $className.".php";
      }

      //require "DBDA.php";
      $dbda = dbda::getDb();
      var_dump($dbda->getAll('select * from login'));

  • 相关阅读:
    服务器消息机制实现记录
    转载SQL经典代码按某一字段分组取最大(小)值所在行的数据
    记录js获取当前URL
    (原创)xilinx IP建立向导创建的目录和文件都是做什么的?由错误ERROR:HDLCompiler:Instantiating <xx> from unknown module <xx>引发的思考
    [转]NTFS3G的安装和配置
    (原创)Notepad++怎么实现双视图/双窗口?
    (原创)Quartus硬件工程路径改变,nios工程该怎么办?
    (原)verilog中的reg类型变量,一定会综合出触发器吗?
    (Windows)使用纯净版本的系统碟安装系统后没有网卡驱动怎么办?
    [转]NIOS_II的Boot过程分析
  • 原文地址:https://www.cnblogs.com/sword082419/p/9144995.html
Copyright © 2020-2023  润新知