<?php
$db = new mysql;
//mysql类
class mysql  {
	function __construct($pconnect=0) {
		global $mysql_host,$mysql_user,$mysql_pass,$mysql_dbname;
		$this->host=$mysql_host;
		$this->user=$mysql_user;
		$this->pass=$mysql_pass;
		$this->dbname=$mysql_dbname;
		$this->link=false;
		$this->querycount=0;
		$this->pconnect=$pconnect;
	}
	function connect() {
		if($this->pconnect==1) {
			$this->link = @mysql_pconnect($this->host, $this->user, $this->pass);
		} else {
			$this->link = @mysql_connect($this->host, $this->user, $this->pass);
		}
		if (empty($this->link)) {
			die('can not connect');
		}
		mysql_select_db($this->dbname);
		if( $this->version() > '4.1' ){
			$this->query("SET NAMES 'GBK'");
		}
	}
	function query($sql) {
		if (empty($this->link)) {
			$this->connect();
		}
		$query = mysql_query($sql);
		if(!$query) {
			die('MySQL Query Error: '.$sql);
		}
		$this->querycount++;
		return $query;
	}
	function select($dbname) {
		return $this->dbname=$dbname;
	}
	function select_db($dbname) {
		return mysql_select_db($dbname);
	}
	function error() {
		return mysql_error();
	}
	function geterrno() {
		return mysql_errno();
	}
	function insert_id() {
		$id = mysql_insert_id();
		return $id;
	}
	function affected_rows($sql)
	{
		$this->query($sql);
		return mysql_affected_rows();
	}
	function fetch_array($query) {
		return mysql_fetch_array($query);
	}
	function fetch_row($query) {
		return mysql_fetch_row($query);
	}
	function num_rows($query) {
		return mysql_num_rows($query);
	}
	function free_result($query) {
		return mysql_free_result($query);
	}
	function close() {
		return mysql_close();
	}
	function version() {
		return mysql_get_server_info();
	}
	function compile_db_insert_string($data)
	{
		$field_names = "";
		$field_values = "";
		foreach ($data as $k => $v)
		{
			$field_names .= "$k,";
			$field_values .= "'$v',";
		}
		$field_names = preg_replace( "/,$/" , "" , $field_names );
		$field_values = preg_replace( "/,$/" , "" , $field_values );
		return array('FIELD_NAMES' => $field_names,
					 'FIELD_VALUES' => $field_values,
					 );
	}
	function compile_db_update_string($data)
	{
		$return_string = "";
		foreach ($data as $k => $v)
		{
			if(is_array($v))
			{
				$return_string .= $k . "=".$v['0'].",";
			}else
			{
				$return_string .= $k . "='".$v."',";
			}
		}
		$return_string = preg_replace( "/,$/" , "" , $return_string );
		return $return_string;
	}
	function insert_sql( $tbl , $arr )
	{
		$dba	=	$this->compile_db_insert_string( $arr );
		$sql	=	"INSERT INTO {$tbl} ({$dba['FIELD_NAMES']}) VALUES ({$dba['FIELD_VALUES']})";
		return $sql;
	}
	function update_sql($tbl , $arr , $where='')
	{
		$dba	=	$this->compile_db_update_string( $arr );
    	$sql = "UPDATE {$tbl} SET {$dba}";
    	if ( $where )
    	{
    		$sql .= " WHERE ".$where;
    	}
		return $sql;
	}
	//以下是常用方法
	//one array
	function get_one($query) {
		$result = $this->query($query);
		return $this->fetch_array($result);
	}
	//one string
	function fetch_one($query) {
		$Re = $this->get_one($query);
		Return $Re[0];
	}
	//all array
	function fetch_all($sql) {
		$arr = array();
		$result = $this->query($sql);
		while($data = $this->fetch_array($result)) {
			$arr[] = $data;
		}
		return $arr;
	}
	//insert
	function insert( $tbl , $arr )
	{
		$dba	=	$this->compile_db_insert_string( $arr );
		$sql	=	"INSERT INTO {$tbl} ({$dba['FIELD_NAMES']}) VALUES ({$dba['FIELD_VALUES']})";
		return $this->query($sql);
	}
	//insert_ignore
	function insert_ignore( $tbl , $arr )
	{
		$dba	=	$this->compile_db_insert_string( $arr );
		$sql	=	"INSERT IGNORE INTO {$tbl} ({$dba['FIELD_NAMES']}) VALUES ({$dba['FIELD_VALUES']})";
		return $this->query($sql);
	}
	//replace into
	function replace( $tbl , $arr )
	{
		$dba	=	$this->compile_db_insert_string( $arr );
		$sql	=	"REPLACE INTO {$tbl} ({$dba['FIELD_NAMES']}) VALUES ({$dba['FIELD_VALUES']})";
		return $this->query($sql);
	}
	//update
	function update($tbl , $arr , $where='')
	{
		$dba	=	$this->compile_db_update_string( $arr );
    	$sql = "UPDATE {$tbl} SET {$dba}";
    	if ( $where )
    	{
    		$sql .= " WHERE ".$where;
    	}
		return $this->query($sql);
	}
}
?>