PDOStatement->fetch());
}
function fetch_array()
{
return($this->PDOStatement->fetch());
}
function fetch_row()
{
return($this->PDOStatement->fetch());
}
}
class Database
{
var $Prefix = '';
var $Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
var $Type = 'mysql'; // mysql, pgsql
var $PDO;
var $Error = '';
var $insert_id;
var $LastQuery = '';
var $ShowSQLError = false;
var $ShowSQLQuery = false;
var $Database = '';
function __construct($Host, $User, $Password, $Database)
{
$this->Database = $Database;
if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
else $ConnectionString = '';
$this->PDO = new PDO($ConnectionString, $User, $Password);
$this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
function select_db($Database)
{
$this->Database = $Database;
$this->query('USE '.$Database);
}
function query($Query)
{
try
{
$this->LastQuery = $Query;
if($this->ShowSQLQuery == true)
echo('
'.$Query.'
'."\n");
$Result = new DatabaseResult();
$Result->PDOStatement = $this->PDO->query($Query);
if($Result->PDOStatement)
$Result->num_rows = $Result->PDOStatement->rowCount();
} catch(PDOException $E)
{
$this->Error = $E->getMessage();
if(($this->Error != '') and ($this->ShowSQLError == true))
echo('SQL Error: '.$this->Error.'
'.$Query.'
');
}
return($Result);
}
function select($Table, $What = '*', $Condition = 1)
{
return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition));
}
function delete($Table, $Condition)
{
$this->PDO->query('DELETE FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition);
}
function insert($Table, $Data)
{
$Name = '';
$Values = '';
foreach($Data as $Key => $Value)
{
$Name .= ',`'.$Key.'`';
if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
$Values .= ','.$Value;
}
$Name = substr($Name, 1);
$Values = substr($Values, 1);
$this->PDO->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
$this->insert_id = $this->PDO->lastInsertId();
}
function update($Table, $Condition, $Data)
{
$Values = '';
foreach($Data as $Key => $Value)
{
if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
$Values .= ', `'.$Key.'`='.$Value;
}
$Values = substr($Values, 2);
$this->PDO->query('UPDATE `'.$this->Prefix.$Table.'` SET '.$Values.' WHERE ('.$Condition.')');
}
function replace($Table, $Data)
{
$Name = '';
$Values = '';
foreach($Data as $Key => $Value)
{
if(!in_array($Value, $this->Functions)) $Value = $this->PDO->quote($Value);
$Name .= ',`'.$Key.'`';
$Values .= ','.$Value;
}
$Name = substr($Name, 1);
$Values = substr($Values, 1);
//echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')
');
$this->PDO->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
//echo($this->error().'
');
}
function charset($Charset)
{
$this->PDO->query('SET NAMES "'.$Charset.'"');
}
function real_escape_string($Text)
{
return(addslashes($Text));
}
}
?>