<?php


class DB_MySQL
{
  private $connection = NULL;
  private $result = NULL;
  private $counter=NULL;
  private $insertid=NULL;
 
 
  public function __construct($database, $user, $pass, $host)
  {
    $this->connection = new mysqli($host, $user, $pass, $database);
    
    if ($this->connection->connect_error) 
    {
        die('Connect Error (' . $this->connection->connect_errno . ') '. $this->connection->connect_error);
    }
    $this->connection->set_charset("utf8");
  }
 
  public function disconnect() 
  {
    if ( $this->connection instanceof mysqli_result)
        mysqli_close($this->connection);
  }
 
 public function insert_id()
 {		
      $this->insertid = mysqli_insert_id($this->connection);
  
  return $this->insertid;
 
 }
 
  public function query($query) 
  {
    $this->result=mysqli_query($this->connection, $query);
  	$this->counter=NULL;
  }
 
  public function fetchRow() 
  {
  	if ( $this->result instanceof mysqli_result)
  	return mysqli_fetch_assoc($this->result);
  }
  
  public function count() 
  {
  	if( ($this->counter==NULL) && ($this->result instanceof mysqli_result) )
    {
  		$this->counter=mysqli_num_rows($this->result);
  	}
  	
	return $this->counter;
  }
  
  public function mysql_escape_string($string) 
  {
	return mysqli_real_escape_string($this->connection, $string);
  }
  
  
}

?>
