<?php

// Extended database class
// Date: 2008-10-13

class Database extends mysqli
{
  var $Prefix = '';
  var $LastQuery = '';
  var $ShowError = 0;

  function query($Query)
  {
    $this->LastQuery = $Query;
    $DbResult = parent::query($Query);
    if(($this->ShowError == TRUE) and ($this->error != ''))
    {
      echo('<strong>Database error:</strong> '.$this->error.'<br /><strong>Query:</strong> '.$Query.'<br />');
      echo('<pre style="font-size: 9px">'); print_r(debug_backtrace()); echo('</pre>');
    }
    return($DbResult);
  }

  function select($Table, $What = '*', $Condition = 1)
  {
    return($this->query("SELECT ".$What." FROM `".$this->Prefix.$Table."` WHERE ".$Condition));
  }

  function delete($Table, $Condition)
  {
    $this->query("DELETE FROM `".$this->Prefix.$Table."` WHERE ".$Condition);
  }

  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->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
  }

  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->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
  }

  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->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
  }

  function charset($Charset)
  {
    $this->query('SET CHARACTER SET '.$Charset);
  }

}

?>
