<?php
!defined("MONKEYMAO") && die("Accessed Defined!");
class db_class{
	var $querytimes=0;
	var $querynum=0;

    var $querylist  = array();
    var $link       = '';
    var $db         = '';
    var $duration   = 0;
    var $timer      = 0;
	function mysql_con($host, $user, $pwd, $dbname, $mode = 1){
		$ctype = $mode ? 'mysql_pconnect' : 'mysql_connect';
		@!$ctype($host, $user, $pwd) && $this -> errorreport("Can not connect MYSQL");
		$this -> select_db($dbname);

		if($mode) {
			if(!$this->link = @mysql_pconnect($host, $user, $pwd)) {
				$halt && $this->errorreport('Can not connect to MySQL server');
			}
		} else {
			if(!$this->link = @mysql_connect($host, $user, $pwd, 1)) {
				$halt && $this->errorreport('Can not connect to MySQL server');
			}
		}

		if($this->version() > '4.1') {
			global $charset, $dbcharset;
			if(!$dbcharset && in_array(strtolower($charset), array('gbk', 'big5', 'utf-8'))) {
				$dbcharset = str_replace('-', '', $charset);
			}

			if($dbcharset) {
				@mysql_query("SET character_set_connection=$dbcharset, character_set_results=$dbcharset, character_set_client=binary", $this->link);
			}

			if($this->version() > '5.0.1') {
				@mysql_query("SET sql_mode=''", $this->link);
			}
		}
		if($dbname) {
			@mysql_select_db($dbname, $this->link);
		}


	}

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

	function query($sql,$type = ''){
		$this->start_timer();
		$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
			'mysql_unbuffered_query' : 'mysql_query';
		!($query = $func($sql)) && $type != 'SILENT' && $this -> errorreport('MySQL Query Error', $sql);
		$this->querynum++;
		$t =$this->stop_timer();
		$this->querytimes =$this->querytimes+$t;
		return $query;
	}



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

	function errno() {
		return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
	}

	function queryarray($sql,$type = '',$rtype = MYSQL_ASSOC){
		$result = $this -> fetch_array($this -> query($sql, $type), MYSQL_ASSOC);
		return $result ? $result : false;
	}
	function select_db($dbname){
		return mysql_select_db($dbname);
	}

	function errorreport($msg='',$sql=''){
		echo $msg.'<br>'.$sql.'<br>'.mysql_error();
	}

	function result($query, $row) {
		return @mysql_result($query, $row);
	}

	function insert_id() {
		return mysql_insert_id();
	}

	function fetch_row($query) {
		return mysql_fetch_row($query);
	}

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

	function num_fields($query) {
		return mysql_num_fields($query);
	}

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

	function close() {
		return mysql_close();
	}

    function start_timer() {
        $mtime = explode(" ", microtime());
        $this->timer = $mtime[1] + $mtime[0];
        return true;
    }

    function stop_timer() {
        $mtime = explode(" ", microtime());
        $endtime = $mtime[1] + $mtime[0];
        $taken = $endtime - $this->timer;
        $this->timer = 0;
        return $taken;
    }
}

?>