class dbstuff {
	var $querynum = 0;
	var $link;
	var $charset;

	var $sqls = array();  //自己添加,存储所有执行的sql语句 uican 2009-12-09
	/**
	 * 连接数据库
	 *
	 * @param string $dbhost
	 * @param string  $dbuser
	 * @param string  $dbpw
	 * @param string  $dbname
	 * @param int    $pconnect 0为非持久连接 1为持久连接
	 * @param bool  $halt
	 */
	function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE) {
		if($pconnect) {
			if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
				$halt && $this->halt('Can not connect to MySQL server');
			}
		} else {
			if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw, 1)) {
				$halt && $this->halt('Can not connect to MySQL server');
			}
		}

		if($this->version() > '4.1') {
			if($this->charset) {
				@mysql_query("SET character_set_connection=$this->charset, character_set_results=$this->charset, 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);
		}
	}

	/**
	 * 选择数据库
	 *
	 * @param string $dbname
	 * @return bool
	 */
	function select_db($dbname) {
		return mysql_select_db($dbname, $this->link);
	}
/**
 * 获取查询的数组
 *
 * @param object $query
 * @param string  $result_type MYSQL_ASSOC 只得到关联索引,MYSQL_NUM 只得到数字索
 * @return array
 */
function fetch_array($query, $result_type = MYSQL_ASSOC) {
	return mysql_fetch_array($query, $result_type);
}

/**
 * 执行一条sql语句
 *
 * @param string  $sql
 * @param string $type 如果$type为UNBUFFERED:则执行mysql_unbuffered_query();他与mysql_query的区别是,执行后不获取和缓存结果的行
 * @return object
 */
function query($sql, $type = '') {
	if(D_BUG) {
		global $_SGLOBAL;
		$sqlstarttime = $sqlendttime = 0;
		$mtime = explode(' ', microtime());
		$sqlstarttime = number_format(($mtime[1] + $mtime[0] - $_SGLOBAL['supe_starttime']), 6) * 1000;
	}
	$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
		'mysql_unbuffered_query' : 'mysql_query';
	if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
		$this->halt('MySQL Query Error', $sql);
	}
	if(D_BUG) {
		$mtime = explode(' ', microtime());
		$sqlendttime = number_format(($mtime[1] + $mtime[0] - $_SGLOBAL['supe_starttime']), 6) * 1000;
		$sqltime = round(($sqlendttime - $sqlstarttime), 3);

		$explain = array();
		$info = mysql_info();
		if($query && preg_match("/^(select )/i", $sql)) {
			$explain = mysql_fetch_assoc(mysql_query('EXPLAIN '.$sql, $this->link));
		}
		$_SGLOBAL['debug_query'][] = array('sql'=>$sql, 'time'=>$sqltime, 'info'=>$info, 'explain'=>$explain);
	}
	$this->querynum++;
	$this->sqls[] = $this->querynum.' '.$sql.'<br/>';
	return $query;
}

/**
 * 取得前一次操作影响的记录数
 *
 * @return int
 */
function affected_rows() {
	return mysql_affected_rows($this->link);
}
	/**
	 * 返回mysql 操作产生的错误信息
	 *
	 * @return string
	 */
	function error() {
		return (($this->link) ? mysql_error($this->link) : mysql_error());
	}

	/**
	 * 返回mysql 操作参数的错误信息的编号
	 *
	 * @return int
	 */
	function errno() {
		return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
	}

	/**
	 * 取得指定结果集
	 *
	 * @param object $query
	 * @param int $row
	 * @return string
	 */
	function result($query, $row) {
		$query = @mysql_result($query, $row);
		return $query;
	}

	/**
	 * 取得结果集的行数
	 *
	 * @param object $query
	 * @return int
	 */
	function num_rows($query) {
		$query = mysql_num_rows($query);
		return $query;
	}

	/**
	 * 取得结果集的字段数
	 *
	 * @param object $query
	 * @return int
	 */
	function num_fields($query) {
		return mysql_num_fields($query);
	}

	/**
	 * 释放结果内存
	 *
	 * @param object $query
	 * @return bool
	 */
	function free_result($query) {
		return mysql_free_result($query);
	}

	/**
	 * 取得上一步插入的id
	 *
	 * @return int
	 */
	function insert_id() {
		return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}

	/**
	 * 从查询结果中取一条记录
	 *
	 * @param object $query
	 * @return array
	 */
	function fetch_row($query) {
		$query = mysql_fetch_row($query);
		return $query;
	}

	/**
	 * 从查询结果中获取列信息
	 *
	 * @param object $query
	 * @return array
	 */
	function fetch_fields($query) {
		return mysql_fetch_field($query);
	}

	/**
	 * Enter 获取mysql 数据库版本
	 *
	 * @return string
	 */
	function version() {
		return mysql_get_server_info($this->link);
	}

	/**
	 * 关闭数据库
	 *
	 * @return bool
	 */
	function close() {
		return mysql_close($this->link);
	}

	/**
	 * 错误提示
	 *
	 * @param string $message
	 * @param string $sql
	 */
	function halt($message = '', $sql = '') {
		$dberror = $this->error();
		$dberrno = $this->errno();
		$help_link = "http://faq.comsenz.com/?type=mysql&dberrno=".rawurlencode($dberrno)."&dberror=".rawurlencode($dberror);
		echo "<div style=\"position:absolute;font-size:11px;font-family:verdana,arial;background:#EBEBEB;padding:0.5em;\">
				<b>MySQL Error</b><br>
				<b>Message</b>: $message<br>
				<b>SQL</b>: $sql<br>
				<b>Error</b>: $dberror<br>
				<b>Errno.</b>: $dberrno<br>
				<a href=\"$help_link\" target=\"_blank\">Click here to seek help.</a>
				</div>";
		exit();
	}
}
posted on 2010-04-29 15:54  小炒花生米  阅读(1091)  评论(0编辑  收藏  举报