Location: PHPKode > scripts > pgsql_dbconnect > pgsql_dbconnect/pgsql_dbconnect.php
<?
class pgsql_dbconnect
{
	/* connection parameters */
	var $host = '';
	var $user = '';
	var $password = '';
	var $dbname = '';
	var $persistent = false;
	
	/* database connection handle */
	var $conn = NULL;
	
	/* query results */
	var $result = false;
	var $row = 0;
	
	function DB()
	{
		/* connection parameters */
		$host = 'host';
		$user = 'user';
		$password = 'password';
		$dbname = 'dbname';
		$persistent = false;
	
		$this->host = $host;
		$this->user = $user;
		$this->password = $password;
		$this->dbname = $dbname;
		$this->persistent = $persistent;
	}
	
	function open()
	{
		/* choose the appropriate connection function */
		if($this->persistent)
			$func = 'pg_connect';
		else
			$func = 'pg_connect';
		
		/* build the connection string */
		$connstr = '';
		if(!empty($this->dbname))
			$connstr .= 'dbname='.$this->dbname.' ';
		if(!empty($this->user))
			$connstr .= 'user='.$this->user.' ';
		if(!empty($this->password))
			$connstr .= 'password='.$this->password.' ';
		if(!empty($this->host))
			$connstr .= 'host='.$this->host.' ';
		
		/* connect to the postgresql server */
		$this->conn = $func($connstr);
		if(!$this->conn)
			return false;
					
		return true;
	}
	
	function close()
	{
		return(pg_close($this->conn));
	}
	
	function error()
	{
		return(pg_errormessage());
	}
	
	function query($sql = '')
	{
		$this->result = pg_exec($this->conn, $sql);
		$this->row = 0;
		
		return($this->result != false);
	}
	
	function affectedRows()
	{
		return(pg_cmdtuples($this->result));
	}
	
	function numRows()
	{
		return(pg_numrows($this->result));
	}
	
	function fetchObject()
	{
		if($this->row >= pg_numrows($this->result))
			return false;
			
		return(pg_fetch_object($this->result, $this->row++, PGSQL_ASSOC));
	}
	
	function fetchArray()
	{
		if($this->row >= pg_numrows($this->result))
			return false;
			
		return(pg_fetch_array($this->result, $this->row++, PGSQL_NUM));
	}
	
	function fetchAssoc()
	{
		if($this->row >= pg_numrows($this->result))
			return false;
			
		return(pg_fetch_array($this->result, $this->row++, PGSQL_ASSOC));	
	}
	
	function freeResult()
	{
		$this->row = 0;
		return(pg_freeresult($this->result));
	}
	
	
}
?>
Return current item: pgsql_dbconnect