<?php

// Extended database class
// Date: 2007-07-19

class database extends mysqli
{
  var $Prefix = '';
  var $LastQuery = '';
  var $LastDataSet;
  var $LastNumRows;
  var $Debug = true;
  
  function query($Query)
  {
    $this->LastQuery = $Query; 
    if ($this->error <> '' and $this->Debug) $this->error.': '.$this->LastQuery;
    return(parent::query($Query));  
  }

  function select($Table, $What = '*', $Condition = 1)
  {
    $this->LastQuery = "SELECT ".$What." FROM `".$this->Prefix.$Table."` WHERE ".$Condition; 
    $resul = $this->query($this->LastQuery);
    if ($this->error <> '' and $this->Debug) echo $this->error.': '.$this->LastQuery;
    
    $this->LastNumRows = $resul->num_rows;
    if ( $this->LastNumRows > 0 ) {
      $this->LastDataSet = true;
    } else {
      $this->LastDataSet = false;
    }
    return($resul);  
  }

  function delete($Table, $Condition)
  {
    $this->LastQuery = "DELETE FROM `".$this->Prefix.$Table."` WHERE ".$Condition;
    $this->query($this->LastQuery);  
    if ($this->error <> '' and $Debug) echo $this->error.': '.$this->LastQuery;
  }
  
  function insert($Table, $Data)
  {
    $Name = '';
    $Values = '';
    foreach($Data as $Key => $Value)
    {
      $Value = strtr($Value, '"', '\"');
      $Name .= ',`'.$Key.'`';
	    if($Value == 'NOW()') $Values .= ",".$Value;
	      else $Values .= ",'".$Value."'";
    }
    $Name = substr($Name, 1);
    $Values = substr($Values, 1);
    $this->LastQuery = 'INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')';
    $this->query($this->LastQuery); 
    if ($this->error <> '' and $Debug) echo $this->error.': '.$this->LastQuery;
  }
  
  function update($Table, $Condition, $Data)
  {
    $Values = '';
    foreach($Data as $Key => $Value)
    {
	    $Value = strtr($Value, '"', '\"');
      if($Value != 'NOW()') $Value = "'".$Value."'";
      $Values .= ", ".$Key."=".$Value;
    }
    $Values = substr($Values, 2);  
    $this->LastQuery = 'UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')';
    $this->query($this->LastQuery);
    if ($this->error <> '' and $this->Debug) echo $this->error.': '.$this->LastQuery;
  }
  
  function replace($Table, $Data)
  {
    $Name = '';
    $Values = '';
    foreach($Data as $Key => $Value)
    {
      $Value = strtr($Value, '"', '\"');
      $Name .= ',`'.$Key.'`';
      if($Value == 'NOW()') $Values .= ",".$Value;
        else $Values .= ',"'.$Value.'"';
    }
    $Name = substr($Name, 1);
    $Values = substr($Values, 1);
    //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br>');
    $this->LastQuery = 'REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')';
    $this->query($this->LastQuery);
    //echo($this->error().'<br>');
  }
  
  function charset($Charset)
  {
    $this->LastQuery = 'SET CHARACTER SET '.$Charset;
    $this->query($this->LastQuery);
  }

}

?>
