<?
$dbbase="newbuy";//MYSQL数据库表名称
$DB=new DB_MYSQL;
$DB->dbServer="localhost"; //MYSQL数据库默认主机
$DB->dbUser="root";  //MYSQL数据库用户名
$DB->dbPwd="";  //MYSQL数据库密码
$DB->dbDatabase="";

class DB_MYSQL
{
  var $dbServer;
  var $dbDatabase;
  var $dbbase;
  var $dbUser; 
  var $dbPwd; 
  var $dbLink;
  var $query_id;
  var $num_rows;
  var $insert_id;
  var $affected_rows;

function connect($dbbase) 
{
global $usepconnect; 
if ($usepconnect==1){
$this->dbLink=@mysql_pconnect($this->dbServer,$this->dbUser,$this->dbPwd);
} else {
$this->dbLink=@mysql_connect($this->dbServer,$this->dbUser,$this->dbPwd);
}
if(!$this->dbLink) $this->halt("主机MYSQL连接出错，请检查会员名及口令是否正确！！！");
if(!mysql_select_db($dbbase, $this->dbLink))
{ $this->halt("MYSQL连接出错，请检查数据库名称是否正确！！！");}
}

function change_db($dbbase){
$this->connect($dbbase);
}
function getid($tables,$dbbase){
if($dbbase!="") $this->change_db($dbbase);
mysql_select_db($tables,$this->dbLink);
$this->getlastid=mysql_insert_id ($this->dbLink);
return $this->getlastid;
}
function query_first($sql,$dbbase){
$query_id=$this->query($sql,$dbbase);
$returnarray=mysql_fetch_array($query_id);
$this->num_rows=mysql_num_rows($query_id);
$this->free_result($query_id);
      return $returnarray;
      }

  function delete($sql,$dbbase){
      $query_id=$this->query($sql,$dbbase);
      $this->affected_rows=mysql_affected_rows($this->dbLink);
      $this->free_result($query_id);
        }

  function insert($sql,$dbbase){
      $query_id=$this->query($sql,$dbbase);
      $this->insert_id=mysql_insert_id($this->dbLink);
      $this->affected_rows=mysql_affected_rows($this->dbLink);
      $this->free_result($query_id);
      return $this->insert_id;
        }

  function update($sql,$dbbase){
      $query_id=$this->query($sql,$dbbase);
      $this->affected_rows=mysql_affected_rows($this->dbLink);
      $this->free_result($query_id);
      }

  function count_records($table,$index="id",$where="",$dbbase){
      if($dbbase!="") $this->change_db($dbbase);
      $result=@mysql_query("select count(".$index.") as 'num' from $table ".$where,$this->dbLink);
      if(!$result) $this->halt("MYSQL语法意外错误: ".$sql);
      @$num = mysql_result($result,0,"num");
      return $num;
      }

  function query($sql,$dbbase){
      if($dbbase!="") $this->change_db($dbbase);
      $this->query_id=@mysql_query($sql,$this->dbLink);
      if(!$this->query_id) $this->halt("MYSQL语法意外错误: ".$sql);
      return $this->query_id;
      }

  function halt($errmsg) 
        {
       $msg="<h3><b>NYSQL数据库链接出错!</b></h3><br>";
       $msg.=$errmsg;
       echo $msg;
       die();
      }

  function free_result($query_id)
        {
        @mysql_free_result($query_id);
        }

  function close()
        {
      mysql_close($this->dbLink);
        }
}
?>