您的位置 >>> 星想互联 >>> 编程技术 >>> PHP基础
基于MYSQLI的数据库操作类
点击数:1479  发布时间2017-09-11 00:03:24

<?php
header("Content-type: text/html; charset=gb2312");
class db{
    public function __construct(){
    $this->myConect();
    }
   
 private $dbInfo=array(
    'host'=>'localhost',
    'port'=>'3306',
    'user'=>'root',
    'pwd'=>'',
    'charset'=>'gb2312',
    'dbname'=>'news',
 );
 
 private $link;
 
 private function myConect(){
  $host=$this->dbInfo['host'];
  $port=$this->dbInfo['port'];
  $user=$this->dbInfo['user'];
  $pwd=$this->dbInfo['pwd'];
  $dbname=$this->dbInfo['dbname'];
  $link=@new mysqli($host,$user,$pwd,$dbname);
  
  if(!$link->connect_error){
   //echo("连接ok!");
   $link->query("set names gb2312");
   $this->link=$link;
   //var_dump($this->link);
  }else{
   echo("连接失败");
  }
  }
 
  public function query($sql){
  if( $stmt=$this->link->query($sql)){
   return $stmt;
  }else{
   echo "数据库操作失败"."<br>";
   echo "失败的操作是:".$sql;
   die;
  }
  }
 
  public function fetchRow($sql){
   $rk=$this->query($sql);
   $row=$rk->fetch_array(MYSQL_ASSOC);
   return $row;
  }
 
  public function fetchAll($sql){
   $rk=$this->query($sql);
   $row=$rk->fetch_all(MYSQL_ASSOC);
   return $row;
  }
}//db类END

$mydb=new db();
$rs=$mydb->fetchAll("");
var_dump($rs);
?>

来源:咸宁网站建设