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


    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
    ", $this->link->error);
    		}
    	}
    	private function __clone(){
    		die('禁止复制对象实例');
    	}
    	private function db_query($sql){
    		$res=$this->link->query($sql);
    		var_dump($sql);
    		if(!$res) printf("命令错误: %s
    ", $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);
    
  • 相关阅读:
    Android自定义控件(2)之组合控件实现新的控件
    Android自定义控件(1)之对现有控件扩展
    Mac OS X 通过sudo启动超级用户权限
    BufferKnife注解式框架
    Android开源框架ViewPageIndicator实现Tab导航
    安卓开发学习经历2--《第一行代码》coolweather项目SQL语句同一个“陷阱”掉两次 注意转义字符等特殊字符正确书写 关于Id字段自增加体会
    安卓开发学习历程1——《第一行代码》coolweather项目setOnItemClickListener函数,Sql语句修改对模拟app程序机影响
    安卓自学经历之创建自定义控件——标题栏
    Qt5 for ubuntu下载
    算法导论---第6章---堆排序
  • 原文地址:https://www.cnblogs.com/zaoa/p/9418955.html
Copyright © 2020-2023  润新知