Home > php教程 > php手册 > 淘宝内部php操作数据库处理类技术含量极高

淘宝内部php操作数据库处理类技术含量极高

WBOY
Release: 2016-06-06 19:46:06
Original
881 people have browsed it

想知道淘宝的那些 技术 人员是怎么工作的吗?他们是怎么管理组织项目的呢?有哪些我们学习之处呢。笔者打算从点点滴滴,由浅入深,从个别到具体的全面的分析一番,目的在于为一起学习共同进步。 从便于维护之角度上讲,OOP是将来必发展。以下此类为php 操作

想知道淘宝的那些技术人员是怎么工作的吗?他们是怎么管理组织项目的呢?有哪些我们学习之处呢。笔者打算从点点滴滴,由浅入深,从个别到具体的全面的分析一番,目的在于为一起学习共同进步。

从便于维护之角度上讲,OOP是将来必发展。以下此类为php操作数据库的常用操作

我们可以看出:1全面使用php6函数,2OOP思想运用的淋漓尽致,3没有一句多余语句,4命名方法科学。

<pre class="brush:php;toolbar:false"><?php class conn{
	public $conn;
	function __construct(){
		$this->conn=mysqli_connect('localhost','root','123123','air');
		mysqli_query($this->conn,'SET NAMES UTF8');
	}
	function __destruct(){
		mysqli_close($this->conn);
	}

	public function delete($table,$condition="") {
        if(empty($condition)) {
            $this->halt('没有设置删除的条件');
            return false;
        }
        $sql = "delete from ".$table." where 1=1 and ".$condition;
        if(!$this->conn->query($sql)){
        	return false;
        }else{
       		return true;
       	}//if
    }
    public function update($table,$dataArray,$condition=""){
    	if(!is_array($dataArray) || (count($dataArray) halt('没有要更新的数据');
    		return false;
    	}
    	$value="";
    	while(list($key,$val)=each($dataArray)){   
    		$value.=$key." = '".$val."',"; 
    	}
    		$value=substr($value,0,-1);
    		$sql="UPDATE ".$table." SET ".$value." WHERE 1=1 AND ".$condition;
    	if(!$this->conn->query($sql)){
    		return false;
    	}else{
    		return true;
    	}    	
    	
	}
    public function insert($table,$dataArray){
    	$field="";
    	$value="";
    	if(!is_array($dataArray) || (count($dataArray) halt('没有要更新的数据');
    		return false;
    	}
    	while (list($key,$val)=each($dataArray)){
    		$field.="$key,";
    	   	$value.="'$val',";
    	}
    	$field=substr($field,0,-1);
    	$value=substr($value,0,-1);
    	$sql="INSERT INTO ".$table." (".$field.") values (".$value." )";
    	if(!$this->conn->query($sql)){
    		return false;
    	}else{
    		return true;
    	}
    }
    public function getOne($sql,$resultType=MYSQL_ASSOC){
    	$q=$this->conn->query($sql);
    	$rt=$q->fetch_array($resultType);
    	return $rt;
    }
    public function getAll($sql){
		$q=$this->conn->query($sql);
		while($r=$q->fetch_array(MYSQLI_USE_RESULT)){
			$rt[]=$r;
		}
		return $rt;    	
    }
   	private function halt($msg='') {
        $msg .= "\r\n".$this->conn->error;
        die($msg);
    }
    public function descTable($t){//显示表结构
		$rt=array();
		$q=$this->conn->query("desc ".$t);
		while($r=$q->fetch_array(MYSQLI_USE_RESULT)){
			$rt[]=$r;
		}
		return $rt;
	}
	public function makeSql($table,$start,$style,$out){//帮助你生成sql 语句

   		$r=$this->getAll("select column_name from information_schema.columns where table_name='".$table."'");
   		$column="(";
   		$value="(";
        $array="";
   		for($i=$start;$i<count switch case break default: if>$_POST["'.$r[$i]["column_name"].'"],';
            }
   		}
   		$column=substr($column,0,-1);
   		$column.=")";
   		$value=substr($value,0,-1);
   		$value.=")";
        switch($out){
            case 0:
                return "INSERT INTO ".$table." ".$column." VALUES ".$value;
            break;
            case 1:
                return $array;
            break;
        }//swith		
	}//makesql
    public function showColumn($table,$start){
        $r=$this->getAll("select column_name from information_schema.columns where table_name='".$table."'");
        $show='<tr>';
        for($i=$start;$i<count>\'.$r["'.$r[$i]["column_name"].'"].\'';
        }
        $show.='</count>
</tr>';
        return $show;
    }//showColumn
}
?></count>
Copy after login
以此类纪念我的丢失的手机,你还有可能回来吗?
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template