sql.interface.php
<?php
/**
* @package SQL
* @author Wim Mariën (http://www.gdx.be/)
* @copyright (c) 2007 Wim Mariën - gdx.be
* @license http://www.opensource.org/licenses/gpl-license.php 
*/
interface sql
{
	public function __construct ($host=NULL, $user=NULL, $pass=NULL, $db=NULL, $port=NULL);
	public function connect ($host, $user, $pass, $db, $port='');
	public function query ($query);
	public function select ($table, $fields=NULL, $wFields=NULL, $wValues=NULL, $order=NULL, $limit=NULL);
	public function insert ($table, $name, $value);
	public function delete ($table, $field=NULL, $value=NULL, $limit=NULL);
	public function fetchobject ($query);
	public function fetcharray ($query);
	public function disconnect ();
	public function __destruct ();
}
?>

mysql.class.php:
<?php
/**
* @package SQL
* @author Wim Mariën (http://www.gdx.be/)
* @copyright (c) 2007 Wim Mariën - gdx.be
* @license http://www.opensource.org/licenses/gpl-license.php 
*/
class mysql implements sql
{
	
	protected $sqlcon;
	public $nQuerys = 0;
	public $fetch;
	
	public function __construct($host=NULL, $user=NULL, $pass=NULL, $db=NULL, $port=NULL)
	{
		if(isset($host) && isset($user) && isset($db))
		{
			$this->connect($host, $user, $pass, $db, $port);
		}
	}
	
	public function connect ($host, $user, $pass, $db, $port='3306')
	{
		$this->sqlcon = mysql_connect($host.':'.$port, $user, $pass) or die('Could not connect to the database server:<br />'.mysql_error());
		mysql_select_db($db, $this->sqlcon) or die('Could not select the database:<br />'.mysql_error());
		return true;
	}
	
	public function query ($query)
	{
		$this->nQuerys++;
		return mysql_query($query);
	}
	
	public function select ($table, $fields=NULL, $wFields=NULL, $wValues=NULL, $order=NULL, $limit=NULL)
	{
		if(is_array($fields))
		{
			$fields = implode(',',$fields);
		}
		
		$strQuery = 'SELECT '.$fields.' FROM '.$table;
		
		if(is_array($wFields) && is_array($wValues) && count($wFields) == count($wValues))
		{
			$strQuery .= ' WHERE ';
			for($i = 0; $i <= count($value)-1; $i++)
			{
				if($i != 0)
					$strQuery .= ' AND ';
				$strQuery .= $field[$i].'=\''.mysql_real_escape_string($value[$i]).'\'';
			}
		}
		elseif(isset($wValues) && isset($wFields))
		{
			$strQuery .= ' WHERE '.$wFields.'=\''.mysql_real_escape_string($wValues).'\'';
		}
		
		if(isset($order))
		{
			$strQuery .= ' ORDER BY '.$order;
		}
		
		if(isset($limit) && is_numeric(str_replace(',', '', $limit)))
		{
			$strQuery .= ' LIMIT '.$limit;
		}
		return $this->query($strQuery);
	}
	
	public function insert ($table, $name, $value)
	{
		if(is_string($table) && is_array($name) && is_array($value) && count($name) == count($value))
		{
			$name = implode(',', $name);
			$values = array();
			foreach($value as $v)
			{
				$values[] = '\''.mysql_real_escape_string($v).'\'';
			}
			$value = implode(',', $values);
		}
		
		return $this->query('INSERT INTO '.$table.' ('.$name.') VALUES ('.$value.')');
	}
	
	public function delete ($table, $field=NULL, $value=NULL, $limit=NULL)
	{
		$strQuery = 'DELETE FROM '.$table;
		if(is_string($field) && isset($value))
		{
			$strQuery .= ' WHERE '.$field.'=\''.mysql_real_escape_string($value).'\'';
		}
		elseif(is_array($field) && is_array($value) && count($field) == count($value))
		{
			$strQuery .= ' WHERE ';
			for($i = 0; $i <= count($value)-1; $i++)
			{
				if($i != 0)
					$strQuery .= ' AND ';
				$strQuery .= $field[$i].'=\''.mysql_real_escape_string($value[$i]).'\'';
			}
		}
		
		if(isset($limit) && is_numeric($limit))
		{
			$strQuery .= ' LIMIT '.$limit;
		}
		
		return $this->query($strQuery);
	}
	
	public function fetchobject ($query)
	{
		return mysql_fetch_object($query);
	}
	
	public function fetcharray ($query)
	{
		return mysql_fetch_array($query);
	}
	
	public function disconnect ()
	{
		@mysql_close($this->sqlcon);
	}
	
	public function __destruct ()
	{
		$this->disconnect();
	}
}
?>

pgsql.class.php:
<?php
/**
* @package SQL
* @author Wim Mariën (http://www.gdx.be/)
* @copyright (c) 2007 Wim Mariën - gdx.be
* @license http://www.opensource.org/licenses/gpl-license.php 
*/
class pgsql implements sql
{
	
	protected $sqlcon;
	public $nQuerys = 0;
	public $fetch;
	
	public function __construct($host=NULL, $user=NULL, $pass=NULL, $db=NULL, $port=NULL)
	{
		if(isset($host) && isset($user) && isset($db))
		{
			$this->connect($host, $user, $pass, $db, $port);
		}
	}
	
	public function connect ($host, $user, $pass, $db, $port='5432')
	{
		$pass = (($pass == NULL) ? '' : ' password='.$pass);
		$this->sqlcon = pg_connect('host='.$host.' user='.$user.' port='.$port.' dbname='.$db.' '.$pass) or die(pg_last_error());
		return true;
	}
	
	public function query ($query)
	{
		$this->nQuerys++;
		return pg_query($this->sqlcon, $query);
	}
	
	public function select ($table, $fields=NULL, $wFields=NULL, $wValues=NULL, $order=NULL, $limit=NULL)
	{
		if(is_array($fields))
		{
			$fields = implode(',',$fields);
		}
		
		$strQuery = 'SELECT '.$fields.' FROM '.$table;
		
		if(is_array($wFields) && is_array($wValues) && count($wFields) == count($wValues))
		{
			$strQuery .= ' WHERE ';
			for($i = 0; $i <= count($value)-1; $i++)
			{
				if($i != 0)
					$strQuery .= ' AND ';
				$strQuery .= $field[$i].'=\''.pg_escape_string($value[$i]).'\'';
			}
		}
		elseif(isset($wValues) && isset($wFields))
		{
			$strQuery .= ' WHERE '.$wFields.'=\''.pg_escape_string($wValues).'\'';
		}
		
		if(isset($order))
		{
			$strQuery .= ' ORDER BY '.$order;
		}
		
		if(isset($limit) && is_numeric(str_replace(',', '', $limit)))
		{
			$strQuery .= ' LIMIT '.$limit;
		}
		return $this->query($strQuery);
	}
	
	public function insert ($table, $name, $value)
	{
		if(is_string($table) && is_array($name) && is_array($value) && count($name) == count($value))
		{
			$name = implode(',', $name);
			$values = array();
			foreach($value as $v)
			{
				$values[] = '\''.pg_escape_string($v).'\'';
			}
			$value = implode(',', $values);
		}
		
		return $this->query('INSERT INTO '.$table.' ('.$name.') VALUES ('.$value.')');
	}
	
	public function delete ($table, $field=NULL, $value=NULL, $limit=NULL)
	{
		$strQuery = 'DELETE FROM '.$table;
		if(is_string($field) && isset($value))
		{
			$strQuery .= ' WHERE '.$field.'=\''.pg_escape_string($value).'\'';
		}
		elseif(is_array($field) && is_array($value) && count($field) == count($value))
		{
			$strQuery .= ' WHERE ';
			for($i = 0; $i <= count($value)-1; $i++)
			{
				if($i != 0)
					$strQuery .= ' AND ';
				$strQuery .= $field[$i].'=\''.pg_escape_string($value[$i]).'\'';
			}
		}
		
		if(isset($limit) && is_numeric($limit))
		{
			$strQuery .= ' LIMIT '.$limit;
		}
		
		return $this->query($strQuery);
	}
	
	public function fetchobject ($query)
	{
		return pg_fetch_object($query);
	}
	
	public function fetcharray ($query)
	{
		return pg_fetch_array($query);
	}
	
	public function disconnect ()
	{
		@pg_close($this->sqlcon);
	}
	
	public function __destruct ()
	{
		$this->disconnect();
	}
}
?>