<?PHP
class DB
{
var $handle;
var $hostname;
var $username;
var $password;
var $persistent;
var $connected;
var $database;
function DB($hostname, $username, $password, $database, $persistent = FALSE)
{
$this->handle = 0;
$this->connected = FALSE;
$this->hostname = $hostname;
$this->password = $password;
$this->username = $username;
$this->persistent = $persistent;
$this->database = $database;
}
function Connect()
{
if( !$this->connected )
{
if( $this->persistent )
{
$this->handle = mysql_pconnect($this->hostname, $this->username, $this->password);
}
else
{
$this->handle = mysql_connect($this->hostname, $this->username, $this->password);
}
$this->SelectDB($this->database);
$this->connected = TRUE;
}
}
function IsConnected()
{
return $this->connected;
}
function Disconnect()
{
if( $this->connected )
{
mysql_close($this->handle);
$this->handle = 0;
$this->connected = FALSE;
}
}
function SelectDB($database)
{
$this->database = $database;
if( !mysql_select_db($this->database, $this->handle) )
{
trigger_error(mysql_error(), E_USER_ERROR);
}
}
function Row($query)
{
$result = mysql_query($query, $this->handle);
if( !$result )
{
trigger_error(mysql_error() . ": $query", E_USER_ERROR);
}
$row = mysql_fetch_array($result);
mysql_free_result($result);
return $row;
}
function Count($query)
{
$result = mysql_query($query, $this->handle);
if( !$result )
{
trigger_error(mysql_error() . ": $query", E_USER_ERROR);
}
$row = mysql_fetch_row($result);
mysql_free_result($result);
return $row[0];
}
function Query($query)
{
$result = mysql_query($query, $this->handle);
if( !$result )
{
trigger_error(mysql_error() . ": $query", E_USER_ERROR);
}
return $result;
}
function Insert($query)
{
$result = mysql_query($query, $this->handle);
if( !$result )
{
trigger_error(mysql_error() . ": $query", E_USER_ERROR);
}
}
function Create($query)
{
$this->Insert($query);
}
function Update($query)
{
$this->Insert($query);
return mysql_affected_rows($this->handle);
}
function BigTables()
{
$this->Insert('SET OPTION SQL_BIG_TABLES=1');
}
function Free($result)
{
mysql_free_result($result);
return;
}
function InsertID()
{
$id = mysql_insert_id($this->handle);
return $id;
}
#REPLACE
function NumRows($result)
{
return mysql_num_rows($result);
}
function NextRow($result)
{
return mysql_fetch_array($result);
}
function Seek($result, $where)
{
mysql_data_seek($result, $where);
}
function UnEscapeHash(&$hash)
{
foreach($hash as $key => $value)
{
if( is_array($hash[$key]) )
{
$this->UnEscapeHash($hash[$key]);
}
else
{
$hash[$key] = stripslashes($value);
}
}
}
function EscapeHash(&$hash)
{
foreach($hash as $key => $value)
{
if( is_array($hash[$key]) )
{
$this->EscapeHash($hash[$key]);
}
else
{
$hash[$key] = $this->Escape($value);
}
}
}
function Escape($string)
{
if( $this->IsConnected() )
{
return mysql_real_escape_string($string, $this->handle);
}
else
{
return mysql_escape_string($string);
}
}
function BackupTables(&$tables, $backup_file)
{
$fd = fopen($backup_file, "w");
foreach( $tables as $table )
{
$result = $this->Query("SELECT * FROM $table");
while( $row = mysql_fetch_row($result) )
{
$items = array();
foreach( $row as $column )
{
$column = $this->Escape($column);
$items[] = "'$column'";
}
fwrite($fd, "INSERT INTO $table VALUES (" . join(',', $items) . ");\n");
}
$this->Free($result);
}
fclose($fd);
Mode($GLOBALS['PERMISSIONS_FILE'], $backup_file);
}
function RestoreTables($restore_file)
{
$statements = file($restore_file);
foreach( $statements as $statement )
{
$statement = preg_replace("/;$/", "", $statement);
mysql_query($statement, $this->handle);
}
}
function GetTables()
{
$tables = array();
$result = $this->Query('SHOW TABLES');
while( $row = $this->NextRow($result) )
{
$tables[$row[0]] = $row[0];
}
$this->Free($result);
return $tables;
}
}
?>