mysqli 面向对象操作数据库的封装类

 

class db{
	private $mysqli;
	private $options;
	private $tableName;
	
	function __construct($tableName){
		$this->tableName=$tableName;
		$this->db();
	}
	private function db(){
		$this->mysqli=new mysqli('localhost','root','','表名字');
		$this->mysqli("set names gbk");
	}
	function fields($fieldsArr){
		if(empty{$fieldsArr}){
			$this->options['fields']='';
		}
		if(is_array($fieldsArr)){
			$this->options['fields']=implode(',',$fieldsArr);
		}else{
			$this->options['fields']=$fieldsArr;
		}
		return $this;
	}
	function order($stt){
		$this->options['order']="order by ".$str;
		return $this;
	}
	function select(){
		$sql="select {$this->options[fields]} from {$this->tableName} {$this->options['order']}";
		return $this->query($sql);
	}
	private function query($sql){
		$result=$this->mysqli->query($sql);
		$rows=array();
		while($row=$result->fetch_assoc()){
			$rows[]=$row;
		}
		return $rows;
	}
	private function close(){
		$this->mysqli->close();
	}
	private function __destruct(){
		$this->close();
	}
}


$channel=new db('channel');
$channelInfo=$channel->fields('id,name,parent_id')->select();
print_r($channel);


 

你可能感兴趣的:(php)