<?php
class DatabaseMysql
{
  var $connectId;
  var $dbName;
  var $errorMsg;
  var $isConnet = 0;    //数据库是否已链接


  
  function connect($dbHost, $dbUser, $dbPassword, $dbName = '', $charset = 'utf8', $pConnect = 0)
  {
    if(0 < $pConnect)
    {
      $this->connectId = @mysql_pconnect($dbHost, $dbUser, $dbPassword, $dbName);
      if(!$this->connectId)
      {
        $this->halt('Can not connect to MySQL Server');
        exit();
      }
    }
    else
    {
      $this->connectId = @mysql_connect($dbHost, $dbUser, $dbPassword, $dbName, 1);
      if(!$this->connectId)
      {
        $this->halt('Can not connect to MySQL Server');
        exit();
      }
    }
    
    if('4.1' < mysql_get_server_info($this->connectId))
    {
      $serverSet = "character_set_connection='$charset', character_set_results='$charset',character_set_client=binary;";
      @mysql_query("set $serverSet ", $this->connectId);
    }
    
    if('5.1' < mysql_get_server_info($this->connectId))
    {
      mysql_query("set sql_mode=''", $this->connectId);
    }
    
    if($dbName && !@mysql_select_db($dbName))
    {
      $this->halt("Cannot use database: $dbName");
      return false;
    }
    
    $this->dbName = $dbName;
    return $this->connectId;
  }
  
  function query($sql, $type='')
  {
    if('UNBUFFERED' == $type)
    {
      $result = mysql_unbuffered_query($sql, $this->connectId);
      if(!result)
      {
        $this->halt('Mysql Query Error', $sql);
        return false;
      }
    }
    else
    {
      $result = mysql_query($sql, $this->connectId);
      if(!$result)
      {
        $this->halt('Mysql Query Error', $sql);
        return false;
      }
    }
    return $result;
  }
  
  function queryFetchArray($sql, $keyField='')
  {
    $arr = array();
    $result = mysql_query($sql);
    while($row = @mysql_fetch_array($result))
    {
      if($keyField)
      {
        $key = $row[$keyField];
        $arr[$key] = $row;
      }
      else
      {
        $arr[] = $row;
      }      
    }
    @mysql_free_result($result);
    return $arr;
  }

  function getList($sql, $keyField='')
  {
    $arr = array();
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
    {
      if($keyField)
      {
        $key = $row[$keyField];
        $arr[$key] = $row;
      }
      else
      {
        $arr[] = $row;
      }      
    }
    mysql_free_result($result);
    return $arr;
  }

  function getRec($sql, $type='')
  {
    $query = $this->query($sql, $type);
    $result = @mysql_fetch_array($query, MYSQL_ASSOC);
    @mysql_free_result($query);
    return $result;
  }

  function getCount($sql, $type='')
  {
    $query = $this->query($sql, $type);
    $result = mysql_fetch_row($query);
    mysql_free_result($query);
    return $result[0];
  }
  
  function insert($tableName, $arr)
  {
    $this->checkField($tableName, $arr);
    return $this->query("insert into $tableName(".implode(',', array_keys($arr)).") values ('".implode("', '", $arr)."');");
  }
  
  function update($tableName, $arr, $where='')
  {
    $this->checkField($tableName, $arr);
    if($where)
    {
      $sql = '';
      foreach($arr as $key=>$value)
      {
        $sql .= ", $key = '$value'";
        $sql = substr($sql, 1);
      }
      $sql = "update $tableName set $sql where $where;";
    }
    else
    {
        $sql .= ", $key = '$value'";
        $sql = substr($sql, 1);
        $sql = "update $tableName set $sql;";
    }
    
    return $this->query($sql);
  }
  
  function getOne($sql, $type='')
  {
    $query = $this->query($sql, $type);
    $result = @mysql_fetch_array($query, MYSQL_ASSOC);
    @mysql_free_result($query);
    return $result;
  }
  
  function fetchArray($result, $resultType = MYSQL_ASSOC)
  {
    return @mysql_fetch_array($result, $resultType);
  }
  
  function fetchRow($result)
  {
    return @mysql_fetch_row($result);
  }
  
  function affectedRows()
  {
    return mysql_affected_rows($this->connectId);
  }
  
  function numRows($result)
  {
    return mysql_num_rows($result);
  }
  
  function numFields($result)
  {
    return mysql_num_fields($result);
  }
  
  function result($result, $row)
  {
    return mysql_result($result, $row);
  }
  
  function checkField($tableName, $arr)
  {
    $tableFields = $this->getTableField($tableName);
    foreach($arr as $key=>$value)
    {
      if(!in_array($key, $tableFields))
      {
        $this->halt("Mysql Qurey Error: Unknown field $key in field list. ");
        return false;
      }
    }
  }
  
  function getTableField($tableName)
  {
    $fields = array();
    $result = $this->query("show columns from $tableName");
    while($row = mysql_fetch_array($result))
    {
      $fields[] = $row['Field'];
    }
    mysql_free_result($result);
    return $fields;
  }
  
  function free_result(&$query)
  {
    return mysql_free_result($query);
  }
  
  function halt($errorMsg = '', $sql = '')
  {
    $this->errorMsg = "Mysql Query: $sql <br />"."Mysql Error: "
                      .mysql_error($this->connectId)."<br>Mysql Error No: ".mysql_errno($this->connectId)
                      ."<br>Error Message: $errorMsg<br>" ;
    echo $this->errorMsg;
    echo "<br><br>For more support, please visit <a href='http://www.kuwebs.com' target=_blank>KuWebs</a>";
  }
  
  function version()
  {
    return mysql_get_server_info($this->connectId);
  }

}

?>