<?php
// +----------------------------------------------------------------------
// 最便捷的php框架
// +----------------------------------------------------------------------
// | Copyright (c) 2011 http://www.ky53.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.ky53.net )
// +----------------------------------------------------------------------

/**
 
 * @author   qq:974005652(zhx626@126.com) by老顽童
 * @version  1.0
 +------------------------------------------------------------------------------
 */
Class MYSQL_DB {
	public function __construct(){
	$conf=require APP_PATH."/".CONFIG_PATH."/config.php";
	$this->connect($conf['DB_PATH'], $conf['DB_USER'], $conf['DB_PWD']);

	}

	function connect($dbpath, $dbuser, $dbpw) {
		global $conn;

		$conn = new com("ADODB.Connection"); 
		$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".realpath($dbpath); 
			
	
		$conn->Open($connstr);
		
		return $conn;
	}
	function exec($sql){
	global $conn;

	$rs = new com("ADODB.RecordSet"); //创建记录集
	
	return $rs->Open($sql,$conn,1,3);//执行查询
	
	}
	function query($sql){
	global $conn;

	$rs = new com("ADODB.RecordSet"); //创建记录集
	
	$rs->Open($sql,$conn,1,3);//执行查询
	
		if(!$rs->eof){
		$count=$rs->Fields->Count();
			
			$j=0;
			while(!$rs->eof) { 
			
			for($i=0;$i<$count;$i++){
    			$f = $rs->Fields($i); 				
    			//$result[]= iconv('gbk','utf-8',$f->value); 
				if($f->value){
				
				//$result[$j][$i]=mb_convert_encoding($f->value,'utf-8');
					
							if(!is_utf8($f->value)){$result[$j][$f->name]=iconv('gbk','utf-8',$f->value); }else{
							
							$result[$j][$f->name]=$f->value;
							}
					

				}else{
				$result[$j][$f->name]=$f->value;
				
				}
				//$result[]=$f->value;
				}
    			$rs->MoveNext(); 
				$j++;
			}
		}
	$rs->close(); 
	
	return $result;
	}
    function select(){
	global $dbpre;
	
	   if($this->field){$fieldstr=$this->field;}else{$fieldstr='*';}
      if($this->where)$str=" where $this->where";
	  if($this->order)$str.=' order by '.$this->order;	
	if($this->limit)$limit=" top ".$this->limit;
	   $this->sql="select $limit $fieldstr from ".$dbpre.$this->table." ".$str;
	  
	 $form=$this->query($this->sql);
	
	 
	 return $form;
	}
	function find(){
	global $dbpre;
	   if($this->field){$fieldstr=$this->field;}else{$fieldstr='*';}
      if($this->where)$str=" where $this->where";
	  if($this->order)$str.=' order by '.$this->order;	
	  if($this->limit)$str.=' limit '.$this->limit;
	   $this->sql="select $fieldstr from ".$dbpre.$this->table." ".$str;
	   
	 $form=$this->query($this->sql);
	
	
	 return $form[0];
	}
	function total(){
	global $dbpre;
      if($this->where)$str=" where ".$this->where;
	 // if($this->order)$str.=' order by '.$this->order;	
	  if($this->limit)$str.=' limit '.$this->limit;
	   $this->sql="select count(*) as total from ".$dbpre.$this->table." ".$str;
	  
	 $form=$this->query($this->sql);
	 
	 
	 return $form[0][total];
	}
	function where($where){
	 $this->where=$where; 
	return $this;
	}
	function order($order){
	 $this->order=$order; 
	return $this;
	}
	function limit($limit){
	 $this->limit=$limit; 
	return $this;
	}
	function field($field){
	 $this->field=$field; 
	return $this;
	}
	function add(){
	global $dbpre;
	$i=1;
	   foreach($this->field as $key=>$k){
	   $fieldstr.="`$key`";
	   if($i<count($this->field))$fieldstr.=',';
	   if($k){
	   $valuestr.="'".iconv('utf-8','gbk',addslashes($k))."'";
	   }else{
	    $valuestr.="'".$k."'";
	   }
	   if($i<count($this->field))$valuestr.=',';
	  $i++;
	   }
	   
	   $this->sql="insert into ".$dbpre.$this->table." ($fieldstr) values ($valuestr)";
	 
	 $form=$this->exec($this->sql);
	 
	 $result=$this->getLastId();
	 return $result;
	}
	 public function getLastId() {
	 
	   global $conn;
$sql="select top 1 * from ".$this->table;
	$rs = new com("ADODB.RecordSet"); //创建记录集
	
	$rs->Open($sql,$conn,1,3);//执行查询
	
		if(!$rs->eof){			
    			$f = $rs->Fields(0); 
				
				$idname= $f->name;				
    			}
			$ids=$this->query("select top 1 $idname from ".$this->table." order by $idname desc");	
    	return $ids[0][$idname];
  	}
	function addAll(){
	global $dbpre;
	$i=1;
	   if($this->field){
	   foreach($this->field as $key=>$k){
	   $fieldstr.=$key.',';
	  
	   
	   $valuestr.="'".$k."',";	   
	 
	  $i++;
	   }
	   }
	   $form=$this->query("select * from ".$dbpre.$this->table);
	   $formarr= $this->fetch_array($form);
	   foreach($formarr as $key=>$k){
	   $fidstr.=$key.',';
	   }
	 $i=1;
	   foreach($_POST as $key=>$k){
	   
	    if(strstr($fidstr,$key)){
		     $fieldstr.=$key.','; 
	         
	         if(strstr($fidstr,$key)) $valuestr.="'".$k."',";	   
	        
			 $i++;
	     }
	   }
	   
	   
	   $this->sql="insert into ".$dbpre.$this->table." (".substr($fieldstr,0,strlen($fieldstr)-1).") values (".substr($valuestr,0,strlen($valuestr)-1).")";
	  
	 $this->query($this->sql);
	 $result=true;
	 return $result;
	}
	
	function save(){
	global $dbpre;
	$i=1;
	   foreach($this->field as $key=>$k){
	   if($k){
	   $fieldstr.=$key.'='."'".iconv('utf-8','gbk',addslashes($k))."',"	; 
	   }else{
	    $fieldstr.=$key.'='."'".$k."',"	; 
		}
	   }
	   if($this->where)$str=" where ".$this->where;
	   
	   $this->sql="update  ".$dbpre.$this->table." set ".substr($fieldstr,0,strlen($fieldstr)-1).$str;
	  
	 $form=$this->exec($this->sql);
	 $result=true;
	 return $result;
	}
	function delete(){
	global $dbpre;
	
	   if($this->where)$str=" where($this->where)";
	   
	   $this->sql="delete from  ".$dbpre.$this->table.$str;
	  
	 $form=$this->query($this->sql);
	 $result=true;
	 return $result;
	}
	function saveAll(){
	global $dbpre;
	
	   if($this->field){
	   foreach($this->field as $key=>$k){
	   $fieldstr.=$key.'='."'".$k."',";
	  
	 
	   }
	   }
	   $form=$this->query("select * from ".$dbpre.$this->table);
	   $formarr= $this->fetch_array($form);
	   foreach($formarr as $key=>$k){
	   $fidstr.=$key.',';
	   }
	 
	   foreach($_POST as $key=>$k){
	   
	    if(strstr($fidstr,$key)){
		    
	         
	         if(strstr($fidstr,$key)) $fieldstr.=$key.'='."'".$k."',"	;   
	        
			
	     }
	   }
	   if($this->where)$str=" where($this->where)";
	   
	   $this->sql="update  ".$dbpre.$this->table." set ".substr($fieldstr,0,strlen($fieldstr)-1).$str;
	  
	 $this->query($this->sql);
	 $result=true;
	 return $result;
	}
	
	
}

?>