<?php
function DB(){
  global $dbhost,$dbuser,$dbpass,$dbname,$pconnect,$charset,$tbl;
  if($GLOBALS['DB']){
    return $GLOBALS['DB'];
  }
  $GLOBALS['DB']=new onezdb($dbhost,$dbuser,$dbpass,$dbname,$pconnect,$charset,$tbl);
  return $GLOBALS['DB'];
}
class onezdb{
	var $version = '';
	var $querynum = 0;
	var $link;
	var $tbl;
	var $dbname;
	
  function onezdb($dbhost,$dbuser,$dbpass,$dbname,$pconnect,$charset,$tbl_){
    
		$func = empty($pconnect) ? 'mysql_connect' : 'mysql_pconnect';
		if(!$this->link = @$func($dbhost, $dbuser, $dbpass)) {
			return false;
		} else {
			@mysql_select_db($dbname, $this->link);
		}
		$this->tbl=$dbname.'.'.$tbl_;
		$this->dbname=$dbname;
		$this->dbname=$dbname;
		@mysql_query('set names utf8', $this->link);
    register_shutdown_function(array(&$this, 'close'));
  }
  
  function reset(){
    @mysql_select_db($this->dbname, $this->link);
  }
  
  function close(){
    mysql_close($this->link);
  }
  
	function version() {
		if(empty($this->version)) {
			$this->version = mysql_get_server_info($this->link);
		}
		return $this->version;
	}

	function error() {
		return (($this->link) ? mysql_error($this->link) : mysql_error());
	}

	function errno() {
		return (($this->link) ? mysql_errno($this->link) : mysql_errno());
	}
  
  function query($sql,$type='') {
    $sql=str_replace('$#',$this->tbl,$sql);
		$query=@mysql_query($sql, $this->link);
		if(!$query)exit($this->errno().'['.$this->error().']');
		$this->querynum++;
		return $query;
  }
	function free_result($query) {
		return @mysql_free_result($query);
	}
  
  function fetch_array($sql) {
    return mysql_fetch_array($sql,MYSQL_ASSOC);
  }
	function insert_id() {
		return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}
  
  function rows($table,$vars="",$field='*') {
    $tbl=$this->tbl;
    $table = $tbl.$table;
    if($vars){
      $vars = "where $vars";
    }
    $result=$this->query("select count($field) as id from $table $vars");
    if(!$result)return 0;
    $rs=$this->fetch_array($result);
    return $rs['id'];
  }
  
  function checkKey($key){
    if(in_array($key,array('force','field','type','name'))){
      $key='`'.$key.'`';
    }
    return $key;
  }
  function insert($table,$arr) {
    $tbl=$this->tbl;
    $table = $tbl.$table;
    $A=array();
    foreach($arr as $k=>$v){
      $k=$this->checkKey($k);
      if(!is_numeric($v)){
        $A[$k]=var_export($v,true);
			}else{
        $A[$k]=$v;
			}
    }
    $this->query("insert into $table (".implode(',',array_keys($A)).")values(".implode(',',array_values($A)).")");
  }
  function replace($table,$arr) {
    $tbl=$this->tbl;
    if(!$arr)return;
    $table = $tbl.$table;
    $A=array();
    foreach($arr as $k=>$v){
      $k=$this->checkKey($k);
      if(!is_numeric($v)){
        $v=var_export($v,true);
        if(substr($v,-2,2)=='\\\'')$v=substr($v,0,-2).'\'';
			}
			$A[]="`$k`=$v";
    }
    $query=$this->query("replace into $table set ".implode(',',$A));
    $this->free_result($query);
  }
  function update($table,$arr,$vars) {
    $tbl=$this->tbl;
    if(!$arr)return;
    $table = $tbl.$table;
    if($vars){
      $vars = "where $vars";
    }
    $A=array();
    foreach($arr as $k=>$v){
      $k=$this->checkKey($k);
      if(!is_numeric($v)){
        $v=var_export($v,true);
        if(substr($v,-2,2)=='\\\'')$v=substr($v,0,-2).'\'';
			}
			$A[]=$k.'='.$v;
    }
    $query=$this->query("update $table set ".implode(',',$A)." $vars");
    $this->free_result($query);
  }
  function delete($table,$vars) {
    $tbl=$this->tbl;
    $table = $tbl.$table;
    if($vars){
      $vars = "where $vars";
    }
    $query=$this->query("delete from $table $vars");
  }
  function select($table,$key,$vars=""){
    $tbl=$this->tbl;
    $table = $tbl.$table;
    if($vars){
      $vars = "where $vars";
    }
    $result=$this->query("select $key from $table $vars");
    if(!$result){
      return false;
    }else{
      $rs=mysql_fetch_array($result);
			$this->free_result($result);
      return $rs[$key];
    }
  }
  function one($table,$key,$vars=""){
    $tbl=$this->tbl;
    $table = $tbl.$table;
    if($vars){
      $vars = "where $vars";
    }
    $result=$this->query("select $key from $table $vars limit 1");
    if(!$result){
      return array();
    }else{
      $rs=$this->fetch_array($result);
			$this->free_result($result);
      return $rs;
    }
  }
  function autoindex($table){
    $tbl=$this->tbl;
    $table = $tbl.$table;
    list($tmp,$table)=explode('.',$table);
    $result=mysql_query("SHOW TABLE STATUS LIKE '$table'");
    if(!$result){
      return false;
    }else{
      $rs=mysql_fetch_array($result);
			$this->free_result($result);
      return $rs['Auto_increment'];
    }
  }
  function record($table,$key,$vars="",$limit=""){
    $tbl=$this->tbl;
    $table = $tbl.$table;
    if($vars){
      $vars = "where $vars";
    }
    if($limit){
      $limit = "limit $limit";
    }
    $k=explode(",",$key);
    $record = Array();
    $result=$this->query("select $key from $table $vars $limit");
    $j=0;
    if(!$result){
      return $record;
    }
    while($onez=$this->fetch_array($result)){
      $record[$j]=$onez;
      $j++;
    }
		$this->free_result($result);
    return $record;
  }
  function page($table,$key,$vars="",$maxperpage=20,$strs){
    strstr($strs,'*')==false && $strs.="&page=*";
    $tbl=$this->tbl;
    $table_=$table;
    $vars_=$vars;
    $pagename = $_SERVER['PHP_SELF'];
    $table = $tbl.$table;
    if($vars){
      $vars = "where $vars";
    }
    $thispage=isset($_GET['page']) ? intval($_GET['page']) : 1;
    if ($thispage=="" || !is_numeric($thispage)){
      $thispage=1;
    }
    $thispage=intval($thispage);
    if($thispage<1)$thispage=1;
    $sql="select $key from $table $vars limit ".(($thispage-1)*$maxperpage).",$maxperpage";
    $result=$this->query($sql);
    $totalput=$this->rows($table_,$vars_);
    if (($totalput %$maxperpage)==0){
      $PageCount=intval($totalput /$maxperpage);
    }else{
      $PageCount=intval($totalput /$maxperpage+1);
    } 
    if($PageCount<$thispage)$thispage=$PageCount;
    $record = Array();
    while($onez=$this->fetch_array($result)){
      $record[]=$onez;
    }
    $ms="";unset($A,$B);
    if($thispage<1)$thispage=1;
    if($PageCount<1)$PageCount=1;
    if($strs && $PageCount>1){
      $buffer = null;
      $index = '首页';
      $pre = '上一页';
      $next = '下一页';
      $last = '末页';
  
      if ($PageCount<=7) { 
        $range = range(1,$PageCount);
      } else {
        $min = $thispage - 3;
        $max = $thispage + 3;
        if ($min < 1) {
          $max += (3-$min);
          $min = 1;
        }
        if ( $max > $PageCount ) {
          $min -= ( $max - $PageCount );
          $max = $PageCount;
        }
        $min = ($min>1) ? $min : 1;
        $range = range($min, $max);
      }
      
      if ($thispage > 1) {
        $buffer .= "<a href='".str_replace('*',1,$strs)."'>{$index}</a> <a href='".str_replace('*',$thispage-1,$strs)."'>{$pre}</a>";
      }
      foreach($range AS $one) {
        if ( $one == $thispage ) {
          $buffer .= "<span>{$one}</span>";
        } else {
          $buffer .= "<a href='".str_replace('*',$one,$strs)."'>{$one}</a>";
        }
      }
      if ($thispage < $PageCount) {
        $buffer .= "<a href='".str_replace('*',$thispage+1,$strs)."'>{$next}</a> <a href='".str_replace('*',$PageCount,$strs)."'>{$last}</a>";
      }
      $page='<div class="page-list">'.$buffer . '</div><div class="clear"></div>';
		}
		
		$this->free_result($result);
    return array($record,$page);
  }
}
?>