«

mysqli扩展访问数据库

时间:2023-3-1 20:59     作者:wen     分类: PHP


<?php
   class DB{
      public $DBhost;
      public $DBuser;
      public $DBpwd;
      public $DBName;
      public $Link;
      public static $_instance;
      private function __construct($n='eduask',$h='localhost',$u='root',$p='12345678'){
         $this->DBhost = $h;
         $this->DBuser = $u;
         $this->DBpwd  = $p;
         $this->DBName = $n;
      }
      function query($sql){
         $result = mysql_query($sql);
         return $result;
      }
      function conn(){
         $this->Link = mysql_connect($this->DBhost,$this->DBuser,$this->DBpwd) or die('数据库登录失败。。。');
         $sql = "set names utf8";
         $this->query($sql);
         mysql_select_db($this->DBName,$this->Link) or die('数据库链接失败');
      }
      function fetch($sql){
         $result = $this->query($sql);
         $arr = array();
         $arr = mysql_fetch_assoc($result);
         return $arr;
      }
      function fetchAll($sql){
         $result = $this->query($sql);
         $arr = array();
         $res = array();
         while($arr = mysql_fetch_assoc($result)){
            $res[] = $arr;
         }
         return $res;
      }
      public static function run(){
         if(self::$_instance==NULL){
            self::$_instance = new DB();
         }
         return self::$_instance;
      } 
      function __destruct(){
         $this->DBhost = NULL;
         $this->DBuser = NULL;
         $this->DBpwd  = NULL;
         $this->DBName = NULL;
      }
   }

?>

标签: mysql