<?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 {

	function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = '') {
		global $dbcharset;
		if($pconnect) {
			if(!@mysql_pconnect($dbhost, $dbuser, $dbpw)) {
				$this->Err('MYSQL 不能永久连接数据库,请确定数据库用户名,密码设置正确,并且服务器支持永久连接<br>');
				
				exit;
			}
		} else {
			if(!@mysql_connect($dbhost, $dbuser, $dbpw)) {				
				$this->Err('MYSQL 连接数据库失败,请确定数据库用户名,密码设置正确<br>');
				
			}
		}
		if(!@mysql_select_db($dbname)){			
			$this->Err("MYSQL 连接成功,但当前使用的数据库 {$dbname} 不存在<br>");
			
			exit;
		}
		if( mysql_get_server_info() > '4.1' ){
			if($dbcharset){
				//mysql_query("SET NAMES '$dbcharset'");
				mysql_query("SET character_set_connection=$dbcharset,character_set_results=$dbcharset,character_set_client=binary");
			}else{
				mysql_query("SET character_set_client=binary");
			}
			if( mysql_get_server_info() > '5.0' ){
				mysql_query("SET sql_mode=''");
			}
		}
		$this->IsConnet=1;
	}

	function close() {
		$this->IsConnet=0;
		return mysql_close();
	}

	function query($SQL,$method='',$showerr='1') {
		if($this->IsConnet==0){
			global $dbhost, $dbuser, $dbpw, $dbname, $pconnect;
			$this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
		}
		
		//分析统计查询时间
		$speed_headtime=explode(' ',microtime());
		$speed_headtime=$speed_headtime[0]+$speed_headtime[1];

		if($method=='U_B' && function_exists('mysql_unbuffered_query')){
			$query = mysql_unbuffered_query($SQL);
		}else{
			$query = mysql_query($SQL);
		}
		
		//分析统计查询时间
		$speed_endtime=explode(' ',microtime());
		$totaltime=number_format((($speed_endtime[0]+$speed_endtime[1]-$speed_headtime)/1),6);
		$speed_totaltime="TIME $totaltime second(s)\t$SQL\r\n";
		if($totaltime>0.3){
			
			if(filesize(ROOT."/cache/MysqlTime.txt")>1024*1024*3){
				unlink(ROOT."/cache/MysqlTime.txt");
			}
		}
		$this->connet_nums++;

		if (!$query&&$showerr=='1')  $this->Err("数据库连接出错:$SQL<br>");
		return $query;
	}

	function get_one($SQL){

		$query=$this->query($SQL,'U_B');
		
		$rs =& mysql_fetch_array($query, MYSQL_ASSOC);

		return $rs;
	}

	

	function fetch_array($query, $result_type = MYSQL_ASSOC) {
		return mysql_fetch_array($query, $result_type);
	}

	function num_rows($query) {
		$rows = mysql_num_rows($query);
		return $rows;
	}

	function free_result($query) {
		return mysql_free_result($query);
	}

	function insert_id() {
		$id = mysql_insert_id();
		return $id;
	}

	
	function Err($msg='') {
		$sqlerror = mysql_error();
		$sqlerrno = mysql_errno();
		if(strstr($sqlerror,"Can't open file: '")){
			preg_match("/Can't open file: '([^']+)\.MYI'/is",$sqlerror,$array);
			echo "系统已自动修复数据库,请再次刷新网页,如果修复不成功,请重启数据库再修复<br>";
			$this->query("REPAIR TABLE `$array[1]`");
		}
		if(strstr($sqlerror,"should be repaired")){
			$sqlerror=str_replace("\\","/",$sqlerror);
			preg_match("/([^\/]+)' is marked as/is",$sqlerror,$array);
			echo "系统已自动修复数据库,请再次刷新网页,如果修复不成功,请重启数据库再修复<br>";
			$this->query("REPAIR TABLE `$array[1]`");
		}
		echo "$msg<br>$sqlerror<br>$sqlerrno";
		//die("");
	}
    function select(){
	global $dbpre;
		$str='';
	   if(!empty($this->field)){$fieldstr=$this->field;}else{$fieldstr='*';}
      if(!empty($this->where))$str=" where($this->where)";
	  if(!empty($this->order))$str.=' order by '.$this->order;	
	  if(!empty($this->limit))$str.=' limit '.$this->limit;
	   $this->sql="select $fieldstr from ".$dbpre.$this->table." ".$str;
	   
	 $form=$this->query($this->sql);
	
	 while($dbarr=$this->fetch_array($form)){	 
	  $dblist[]=$dbarr;
	   }
	 return $dblist;
	}
	function find(){
	global $dbpre;
	$str='';
	   if(!empty($this->field)){$fieldstr=$this->field;}else{$fieldstr='*';}
      if(!empty($this->where))$str=" where($this->where)";
	  if(!empty($this->order))$str.=' order by '.$this->order;	
	  if(!empty($this->limit))$str.=' limit '.$this->limit;
	   $this->sql="select $fieldstr from ".$dbpre.$this->table." ".$str;
	   
	 $form=$this->query($this->sql);
	
	 while($dbarr=$this->fetch_array($form)){	 
	  $dblist[]=$dbarr;
	   }
	 return $dblist[0];
	}
	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.=',';
	   
	   $valuestr.="'".$k."'";
	   if($i<count($this->field))$valuestr.=',';
	  $i++;
	   }
	   
	   $this->sql="insert into ".$dbpre.$this->table." ($fieldstr) values ($valuestr)";
	  
	 $form=$this->query($this->sql);
	 $result=true;
	 return $result;
	}
	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){
	   $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->query($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;
	}
	
	
}

?>