for any other database type.
if($dbObjDefined != 1)
{
$dbObjDefined = 1;
//Wrapper class for database calls
class dbObj
{
//Connection handle to database
var $conn;
//Default connection parameters
var $host = "YourSite.com";
var $user = "johndoe";
var $password = "pwd";
var $port = "5432";
var $dbname = "MyDB";
//Open initial connection. $params is an associative array holding
//parameters to the pg_Connect function.
function init($params)
{
if(isset($parame[host]))
$host = $parame[host];
else
$host = $this->host;
if(isset($parame[user]))
$user = $parame[user];
else
$user = $this->user;
if(isset($parame[password]))
$password = $parame[password];
else
$password = $this->password;
if(isset($parame[port]))
$port = $parame[port];
else
$port = $this->port;
if(isset($parame[dbname]))
$dbname = $parame[dbname];
else
$dbname = $this->dbname;
$this->conn = pg_Connect ( " host=$host user=$user password=$password port=$port dbname=$dbname
");
}
//Send SQL to database connection.
//Return recordset object on success.
//Return 0 on failure.
function exec($SQL)
{
$resultset = pg_Exec($this->conn, $SQL);
if ($resultset) {
$recset = new recordset;
$recset->init($resultset);
return $recset;
}
else {
return 0;
}
}
//Close connection to database
function free()
{
pg_close($this->conn);
}
};
/*
This is a simple recordset class which can be
traversed using next(), prev(), and current() methods.
It is initialized from a resultset returned from the
function "pg_Exec" or can be generated by a call to the
exec method from the dbObj.
*/
class recordset
{
var $resultset;
var $index;
var $numFields;
var $numTuples;
function init($newResultset)
{
$this->resultset = $newResultset;
$this->index = 0;
$this->numFields = pg_NumFields($this->resultset);
$this->numTuples = pg_NumRows($this->resultset);
}
//Get a value by row name and either column name or column number
function getVal($row, $col)
{
return pg_Result($this->resultset, $row, $col);
}
//Return an array of field names
function getFields()
{
for($i=0; $i<$this->numFields; $i++)
$retArray[] = pg_FieldName($this->resultset, $i);
return $retArray;
}
//Get number of columns in resultset
function getNumFields()
{
return $this->numFields;
}
//Get a tuple (associative array of column values) by row number
function getTupleDirect($row)
{
for($i=0; $i<$this->numFields; $i++)
$retArray[pg_FieldName($this->resultset, $i)] =
pg_Result($this->resultset, $row, $i);
return $retArray;
}
//Get tuple pointed to by the current index
function getTuple()
{
if($this->index>=0 && $this->index < $this->numTuples)
return $this->getTupleDirect($this->index);
else
return 0;
}
//Get an array filled with all values in a column
//(using either column name or column number)
function getColumn($col)
{
for($i=0; $i<$this->numTuples; $i++)
$retArray[] = pg_Result($this->resultset, $i, $col);
return $retArray;
}
//Return the number of records in the recordset
function getNumTuples()
{
return $this->numTuples;
}
//Return 1 if index is within bounds of the recordset
function current()
{
if($this->index>=0 && $this->index < $this->numTuples)
return 1;
else
return 0;
}
//Incriment index
function next()
{
if($this->index<$this->numTuples)
{
$this->index++;
return 1;
}
else
{
return 0;
}
}
//Decriment index
function prev()
{
if($this->index >= 0)
{
$this->index--;
return 1;
}
else
{
return 0;
}
}
//Reset index to 0
function reset()
{
$this->index = 0;
}
//Free memory allocated to recordset.
function free()
{
pg_Freeresult($this->resultset);
}
};
}
?>