class Mysql{
private $host;
private $user;
private $pwd;
private $dbName;
private $charset;
private $conn=null;
public function __construct(){
$this->host='localhost';
$this->user='root';
$this->pwd='111111';
$this->dbName='test';
$this->connect($this->host,$this->user,$this->pwd);
$this->switchDb($this->dbName);
$this->setChar($this->charset);
}
//负责连接数据库
private function connect($h,$u,$p){
$conn=mysqli_connect($h,$u,$p);
$this->conn=$conn;
}
//负责切换库
private function switchDb($db){
$sql='use '.$db;
$this->query($sql);
}
//负责设置字符集
public function setChar($char){
$sql='set names '.$char;
$this->query($sql);
}
//负责发送sql查询
public function query($sql){
return mysqli_query($this->conn,$sql);
}
//获取多行多列的select结果
public function getAll($sql){
$list=array();
$rs=$this->query($sql);
if(!$rs){
return false;
}
while($row=mysqli_fetch_assoc($rs)){
$list[]=$row;
}
return $list;
}
//获取一行的select结果
public function getRow($sql){
$rs=$this->query($sql);
if(!$rs){
return false;
}
return mysqli_fetch_assoc($rs);
}
//获取一个单一的值
public function getOne($sql){
$rs=$this->query($sql);
if(!$rs){
return false;
}
$row=mysql_fetch_row($rs);
return $row[0];
}
public function close(){
mysqli_close($this->conn);
}
}