<?php
// +---------------------------------------------+
// |     Copyright  2003 - 2005 weenCompany      |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

class DB
{
  var $server   = "localhost";
  var $user     = "root";
  var $password = "";
  var $database = "";

  var $conn     = 0;
  var $queryid  = 0;
  var $row   = array();

  var $errdesc  = "";
  var $errno   = 0;
  
  var $query_count  = 0;


  // ###################### connect #######################
  function connect()
  {
    if(0 == $this->conn)
    {
      if($this->password=="")
      {
        $this->conn = mysql_connect($this->server,$this->user);
      }
      else
      {
        $this->conn = mysql_connect($this->server,$this->user,$this->password);
      }

       $sqlversion = @mysql_get_server_info();
	   if(empty($sqlversion)){$sqlversion='5.0';}
		
       if($sqlversion >= '4.1'){
           mysql_query("set names 'gbk'");
           
       }

       if($sqlversion >= '5.0') {
           mysql_query("SET sql_mode=''");
       }

      if(!$this->conn)
      {
        $this->error("Connection == false, connect failed");
      }

      if($this->database != "")
      {
        if(!mysql_select_db($this->database, $this->conn))
        {
          $this->error("cannot use database ".$this->database);
        }
      }
    }
  }



  // ###################### select database #######################
  function select_db($database = "")
  {
    if($database != "")
    {
      $this->database = $database;
    }

    if(!mysql_select_db($this->database, $this->conn))
    {
      $this->error("cannot use database ".$this->database);
    }
  }



  // ###################### query #######################
  function query($query_string)
  {
    $this->queryid = mysql_query($query_string,$this->conn);

    if (!$this->queryid)
    {
      $this->error("Invalid SQL: ".$query_string);
    }
    $this->query_count++;

    return $this->queryid;
  }


  // ###################### query first #######################
  function query_first($query_string)
  {
    $queryid    = $this->query($query_string);
    $returnarray = $this->fetch_array($queryid, $query_string);

    $this->free_result($queryid);

    return $returnarray;
  }



  // ###################### fetch array #######################
  function fetch_array($queryid=-1,$query_string="")
  {
    if($queryid != -1)
    {
      $this->queryid=$queryid;
    }
	
    if(isset($this->queryid))
    {
      if(($this->row = mysql_fetch_array($this->queryid)) === FALSE)
	  {
	    return null;
	  }
    }
    else
    {
      if(!empty($query_string))
      {
        $this->error("Invalid query id (".$this->queryid.") on query string: $query_string");
      }
      else
      {
        $this->error("Invalid query id: ".$this->queryid);
      }
    }

    return $this->row;
  }

  // ###################### fetch array #######################
  function affected_rows()
  {
        if($this->conn)
        {
                $result = @mysql_affected_rows($this->conn);
                return $result;
        }
        else
        {
                return false;
        }
  }



  // ###################### free result #######################
  function free_result($queryid=-1)
  {
    if($queryid != -1)
    {
      $this->queryid = $queryid;
    }
    return @mysql_free_result($this->queryid);
  }



  // ###################### number of rows #######################
  function get_num_rows()
  {
    return mysql_num_rows($this->queryid);
  }

  
  // ###################### number of fields #######################
  function get_num_fields()
  {
    return mysql_num_fields($this->queryid);
  }


  // ############ return last auto_increment number ##############
  function insert_id()
  {
    return mysql_insert_id($this->conn);
  }



  // ###################### close the connection to the database #######################
  function close()
  {
    return mysql_close($this->conn);
  }



  // ###################### get error description #######################
  function geterrdesc()
  {
    $this->error = mysql_error();
    return $this->error;
  }



  // ###################### get error number #######################
  function geterrno()
  {
    $this->errno = mysql_errno();
    return $this->errno;
  }


  // ###################### error message #######################
  function error($msg)
  {
    global $cwsversion;
	$cwsversion = 'V'. $cwsversion .' (Chinese/English GB2312)';

    $this->errdesc = mysql_error();
    $this->errno   = mysql_errno();

    $message  = "Database error in weenCompany " . $cwsversion . EMAIL_CRLF . EMAIL_CRLF;
    $message .= $msg." " . EMAIL_CRLF;
    $message .= "Error: ".$this->errdesc." " . EMAIL_CRLF;
    $message .= "Error number: ".$this->errno." " . EMAIL_CRLF;
    $message .= "Date: ".gmdate("l dS of F Y h:i:s A"). EMAIL_CRLF;
    $message .= "File: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

    echo '<b>Database Error.</b> <br />
          <p><form><textarea rows="15" cols="60">'.htmlspecialchars($message).'</textarea></form></p>';

    exit;
  }

}

?>