Submitted by: Barton Greg greg@createtech.com To get this file, in the web-browser, save this file as 'Text' type as pgsql.lib
This is a database wrapper for PostgreSQL, but can be simply modified for any other database type. <?php 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) { $this->resultset = pg_Exec($this->conn, $SQL); if ($this->resultset) { $recset = new recordset; $recset->init($this->resultset); return $recset; } else { return 0; } } function valid() { return $this->resultset; } // 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 given above. ** Below "Tuples" means rows. */ 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); } // Used in display() below function valid() { return $this->resultset; } // Get a value by row number 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 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; } // 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; } function valueof($col) { if ($col < $this->numFields) { return pg_Result($this->resultset, $this->index, $col); } else { return ""; } } // Reached last row - end of rows ? Used in display() below function eof() { return $this->index == $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; } // Increment index. Used in display() below function next() { if ($this->index < $this->numTuples) { $this->index++; return 1; } else { return 0; } } // Decrement index function prev() { if ($this->index >= 0) { $this->index--; return 1; } else { return 0; } } // Reset index to 0 - See also first() function reset() { $this->index = 0; } // See also reset(). Used in display() below function first() { $this->index = 0; } function last() { $this->index = $this->numTuples -1 ; } // Used in display() below function showheader($col, $fmt = "") { printf("\t< th %s>%s< /th >\n", $fmt, is_string($col) ? $col : pg_fieldname($this->resultset, $col)); } // Used in display() below function showvalue($col, $fmt = "", $def = " ") { $v = $this->valueof($col); printf( "\t< td %s>%s< /td>\n", $fmt, $v == "" ? $def : $v); } function showurl($col, $fmt = "") { $v = $this->valueof($col); if ( $v != "" ) { printf("\t< td %s> < /td>\n", $fmt); } else { printf( "\t< td %s>< a href=%s>%s< /a>< /td>\n", $fmt, $v, $v); } } function display() { if (!$this->valid() ) { return; } printf( "<table cellspacing=1 cellpadding=1 border=1>\n"); printf( "<tr>\n"); for ($c = 0; $c < $this->cols; $c++ ) { $this->showheader($c); } printf( "< /tr>\n"); $this->first(); while (!$this->eof()) { printf( "<tr>\n"); for ($c = 0; $c < $this->cols; $c++) { $this->showvalue($c); } printf( "< /tr>\n"); $this->next(); } printf("< /table\n"); } // Free memory allocated to recordset. function free() { pg_Freeresult($this->resultset); } }; } ?>