<?php 
class D{
public $dbname;
public $where;
public $order;
public $limit;
public $link;

function __construct($db){
		$this->dbname = $db;
	}  
function where($arr){
	if($arr != NULL){
	if(is_array($arr)){
		foreach($arr as $key => $value)
		{
			$sqlwhere[] = ' '.$key.'=\''.$value.'\' ';
		}
		$this->where = ' WHERE '.implode("AND",$sqlwhere);
		}else{
		$this->where =' '.$this->getfield(0).'='.$arr.' ';
		}
	}
	return $this;
}

function order($str){
	if($str != NULL){
		$this->order = " ORDER BY {$str} ";
	}
	return $this;
}

function limit($value1,$value2=null){
	if($value2!=NULL){
		$limit = $value1.",".$value2;
		$this->limit = " LIMIT {$limit} ";
	}elseif($value2 == NULL){
		$limit = $value1;
		$this->limit = " LIMIT {$limit} ";
	}
	return $this;
}

function select(){
	//echo "SELECT * FROM {$this->dbname}{$this->where}{$this->link}{$this->order}{$this->limit};";
	$sql_array = mysql_query("SELECT * FROM {$this->dbname}{$this->where}{$this->link}{$this->order}{$this->limit};");
	/*获取查询的字段*/
	for($i=0;$i<mysql_num_fields($sql_array);$i++){
        $meta = mysql_fetch_field($sql_array);
        $fields[$i] = $meta->name;
    }
	/*遍历每一条记录*/
	$i = 0;
	if($sql_array && mysql_num_rows($sql_array) > 0){
		while($array_value = mysql_fetch_row($sql_array)){
			/*遍历每一个值*/
			$n = 0;
			foreach($array_value as $value){
			$f = $fields[$n];
			/*把值对应值三维数据*/
			$array[$i][$f] = $value;
			$n++;
			}
			$i++;
		}
		return $array;
	}else{
		return FALSE;
	}
}

function like($arr){
	if($arr != NULL){
	if(is_array($arr)){
		foreach($arr as $key => $value)
		{
			$sqlwhere[] = ' '.$key.' LINK %\''.$value.'\'% ';
		}
		$where = $this->where() == null ? 'WHERE' : 'AND';
		$this->link = ' '.$where.' '.implode("AND",$sqlwhere);
		}else{
		$this->link =' '.$where.' '.$this->getfield(0).' LINK %'.$arr.'% ';
		}
	}
	return $this;
}
function find($id=null){
	$where = $this->where;
	if($this->where == NULL){
		$where = ' '.$this->getfield().'='.$id.' ';
	}
	$sql = "SELECT * FROM {$this->dbname}{$where}{$this->link}{$this->order}{$this->limit};";
	$res = mysql_query($sql);
	if($res && mysql_num_rows($res)>0){
		return mysql_fetch_array($res);
	}
	return FALSE;
}    
function update($arr,$id){
	$where = $this->where;
	if($this->where == NULL){
		$where = ' '.$this->getfield(0).'='.$id.' ';
	}
	if($arr != NULL){
		foreach($arr as $key=>$value){
			$sqlu[] = " {$key}='{$value}' "; 
		}
	}else{
		foreach($_POST as $key=>$value)
		{
			$sqlu[] = " {$key}='{$value}' "; 
		}
	}
	$zd = implode(",",$sqlu);
	$sql = "UPDATE {$this->dbname} SET {$zd}{$this->where}{$this->order}{$this->limit} ";
	$res = mysql_query($sql);
	if($res && mysql_affected_rows()>0){
		return TRUE;
	}else{
		return FALSE;
	}
}

function delete($id){
	$where = $this->where;
	if($this->where == NULL){
		$where = ' '.$this->getfield().'='.$id.' ';
	}
	$res = mysql_query("SELETE FROM {$this->dbname}{$where}{$this->link}{$this->order}{$this->limit}");
	if($res && mysql_affected_rows()>0){
		return TRUE;
	}else{
		return FALSE;
		}
}
function insert($sql){
	if($sql == NULL)
	{
	$sql = $_POST;
	}
	$sql_sel = "SELECT * FROM {$this->dbname} LIMIT 1";
	$sql_array = mysql_query($sql_sel);
	for($i=0;$i<mysql_num_fields($sql_array);$i++){
        $meta = mysql_fetch_field($sql_array);
        $fields[] = $meta->name;
		if($sql[$meta->name] != NULL){
			$sqlarr[] = '\''.$sql[$meta->name].'\'';
		}else{
			if($i == 0){
			$sqlarr[] = 'NULL';
			}else{
			$sqlarr[] = '\'\'';
			}
		}
    }
	$zd     =  implode(",",$fields);
	$value  =  implode(",",$sqlarr);
	$sqlsert="INSERT INTO {$this->dbname}({$zd})VALUES({$value})";
	//echo $sqlsert;
	$res = mysql_query($sqlsert);
    if($res && mysql_affected_rows()>0){
		return mysql_insert_id();
	}else{
		return FALSE;
	}
}

function getfield($is){
		$dbname = $this->dbname;
		$sql_array = mysql_query("SELECT * FORM {$dbname} LIMT 1");
		for($i=0;$i<mysql_num_fields($sql_array);$i++)
		{
			$meta = mysql_fetch_field($sql_array);
			$fields[$i] = $meta->name;
		}
		if($s==NULL){
			return $field;
		}
		return $field[$is];
	}
function getnums(){
/*Class End*/
	$sql = "SELECT * FROM {$this->dbname}{$this->where}{$this->limit};";
	$nums = mysql_num_rows(mysql_query($sql));
	if($nums != NULL)
	{
		return $nums;
	}
	return '0';
}


}