PHP单表操作mysqli数据库类的封装

时间:2022-12-11 10:36:05
class DB{
	private $options=array(
		 'database_type' => 'mysql',
    		'database_name' => 'test',
    		'server' => 'localhost',
    		'username' => 'root',
   		 'password' => '',
    		'charset' => 'utf8'
	);
	private $link;
	static private $instance=null;
	static $table;
	private function __construct(){		
		$this->db_connect();
		$this->db_setcharset();
	}
	static public function getInstance($table){
		if(!$table) exit('请输入要操作的数据表名');
		self::$table=$table;
		if (!self::$instance instanceof self) {
           			 self::$instance = new self($config);
         		}
	         return self::$instance;
	}
	private function db_connect(){
		$options=$this->options;
		    try {
        			$this->link = new mysqli($options['server'], $options['username'], $options['password'],$options['database_name']);
    			}
    		catch (Exception $e) {
        			die("数据库连接失败".$e);
    		}
	}
	private function db_setcharset(){
		$options=$this->options;
		if (!$this->link->set_charset($options['charset'])) {
    			printf("Error loading  : %s\n", $this->link->error);
		}
	}
	private function __clone(){
		die('禁止复制对象实例');
	}
	private function db_query($sql){
		$res=$this->link->query($sql);
		var_dump($sql);
		if(!$res) printf("命令错误: %s\n", $mysqli->error);
		return $res;
	}
	public function insert($data){
		$keys=join(",",array_keys($data));
		foreach ($data as $key => $value) {
			# code...
			$value=$this->link->escape_string($value);
		}
		$values="'".join("','",array_values($data))."'";
		$sql="INSERT INTO ".self::$table." ({$keys}) VALUES ({$values})";
		$res=$this->db_query($sql);
		return $this->link->affected_rows;
	}
	public function delete($where=null,$tag='and'){
		// $where=array("age<28")
		$condation=$this->dealWhere($where,$tag);
		$sql="delete from ".self::$table.($where==null?null:" where ". $condation);;
		$res=$this->db_query($sql);
		return $this->link->affected_rows;
	}
	private function dealWhere($where,$tag){
		if(is_array($where)&&count($where)>0){
			foreach ($where as $key => $value) {
				$condation.=' '.$value.' '.$tag;
			}
			$condation=trim($condation,$tag);
		}elseif(!empty($where)){
			$condation=$where;
		}else{
			$condation='';
		}
		return $condation;
	}
	public function update($data,$where=null,$tag='and'){
		// $where=array("age<28")
		$condation=$this->dealWhere($where,$tag);
		if(is_array($where)&&count($where)>0){
			foreach ($where as $key => $value) {
				$condation.=' '.$value.' '.$tag;
			}
			$condation=trim($condation,$tag);
		}elseif(!empty($where)){
			$condation=$where;
		}
		$values='';
		foreach ($data as $key => $value) {
			# code...
			echo $key;
			$value=$this->link->escape_string($value);
			$values.=$key."='".$value."',";
		}
		$values=trim($values,',');
		$sql="update  ".self::$table." set {$values}".($where==null?null:" where ". $condation);
		$res=$this->db_query($sql);
		return $this->link->affected_rows;
	}
	public function select($where=null,$filed='*',$tag='and',$order=null,$limit=null){
		$condation=$this->dealWhere($where,$tag);
		$sql="select  {$filed} from ".self::$table.($where==null?null:" where ". $condation).($order==null?null:" order by ". $order).($limit==null?null:" limit ". $limit);
		$res=$this->db_query($sql);
		$rows=[];
		 while ($row = $res->fetch_assoc()) {
        			$rows[]=$row;
    			}
		return $rows;	
	}

}
header('content-type:text/html;charset=utf-8');
$db=DB::getInstance('user');
//$insert_id=$db->update(array('username'=>'hello','age'=>30),'id=14');
$rows=$db->select('id>2','*','and',null,'4');
var_dump($rows);