Version: 1.0
Type: Full Script
Category: Databases
License: GNU General Public License
Description: This is a database abstraction class for use with PHP3/4 and PostgreSQL (see TITLE).
This class uses internal error handling (e.g: if a query screws up, it won’t be outputted thru the server, so end users won’t see ugly SQL error messages. Also, all functions are interrelated to one and other
resultId’s connectionId’s and the like are
all handled internally by the class, so one you execute a query, you can find out the number of rows returned in one function call with no parameters.
Who would wanna use this class:
1) People who do not want sql error output
to the browser
2) People who want customized error
handling
3) Developers who want a well designed
database abstraction class.
Who wouldn’t want it:
1) People who don’t like to handle
errors (but if you design you database
/ query’s well, you should have no
problem.
Anyhow, enjoy! (im working on a even better version)
<?php /***************************************************************************************************** * PostgreSQL Connection / Query Class * * * * Purpose: To encapsulate PostgreSQL Connections and Query's in an easy to use class, with error * * handling at the object level. * * * * Programmed by Andrew Tejero * ***************************************************************************************************** Usage Example: $dbObject = new psqlDb; if ($dbObject->dbLink("dbname=example")) { // Connection is good if ($dbObject->dbQuery("SELECT * FROM names WHERE firstname='Andrew')){ // Query Succeeded print("<table>n" print("<tr>n"); $innerLoops = $dbObject->dbGetNumFields(); do { print("<td>" . $dbObject->dbGetFieldName($innerLoops) . "</td>"); } while ($counter < $innerLoops); print(</tr>n"); for ($loopCounter = 0; $loopCounter < $dbObject->dbNumRows(); $loopCounter++) { $rowReturn = $dbObject->dbGetRow($loopCounter); print("<tr>n"); for($iLoop = 0; $iLoop < $innerLoops; $iLoop++) { print("<td>" . $rowReturn($iLoop); . "</td>n"); } print("</tr>n"); } } else { // Query Failed... (either because of incorrect syntax or internal error) // we'll only display output for the latter. print("nError: " . $dbObject->dbGetErrors() . "!"); } print("<table>"); $dbObject->dbUnLink(); } else if (($dbObject->dbLink("dbname=example") == -1) { // Error connecting to db print("nError: " . $dbObject->dbGetErrors() . "!"); } else { // dbLink() was not called properly print("ndbLink() has not been called properlyn"); } */ class psqlDb{ /*************************************************************************************************** * Members * ***************************************************************************************************/ var $connId; /* Database Connection ID */ var $resultId; /* Database query return ID */ var $dbError; /* Keeps error status of pg_<functions> */ /*************************************************************************************************** * Methods * ***************************************************************************************************/ function dbLink($dbConnString){ if (isset($connId)) { /* There's already a connection ID, so lets disconnect from the database and set the new conn id */ $this->dbUnLink(); } if (isset($dbConnString)) { $conVar = @ pg_connect($dbConnString); if ($conVar) { $this->connId = $conVar; return(1); } else { /* Uh oh! Error connecting to the db, so set $dbError, and return an error code */ $this->__IsError(); return(-1); } } else { /* This function wasn't called properly, set exit status -2 */ return(-2); } } function dbUnlink(){ if (isset($this->connId)){ pg_close($this->connId); return(1); } else { /* The function wasn't called right, so we return -1 */ return(-1); } } function dbQuery($dbQueryString){ /* Send a query out to the database */ if (isset($this->connId) && isset($dbQueryString)){ $returnVar = @ pg_exec($this->connId,$dbQueryString); if ($returnVar){ $this->resultId = $returnVar; return(1); } else { /* The query didn't succeed for some reason, so we set dbError and return an error code */ $this->__IsError(); return(-1); } } else { return(-1); } } function dbNumRows() { if (isset($this->connId) && isset($this->resultId)) { $numRowVar = @ pg_numrows($this->resultId); if ($numRowVar) { return($numRowVar); } else { /* Something happened, set dbError, and return an error code */ $this->__IsError(); return(-1); } } else { /* Return the standard "function not called right" return code */ return(-2); } } function dbGetRow($row) { if (isset($this->connId) && isset($this->resultId) && ($row < $this->dbNumRows())) { $getRowVar = @ pg_fetch_array($this->resultId,$row); if ($getRowVar) { return($getRowVar); } else { /* We seem to have an error returning the number of rows */ $this->__IsError(); return(-1); } } else { /* No conn id, result id or $row too big... set exit status -2 */ return(-2); } } function dbGetNumFields() { if (isset($this->connId) && isset($this-> resultId)) { $numFieldVar = @ pg_numfields($this->resultId); if ($numFieldVar) { return($numFieldVar); } else { /* Error due to some backend or connection problem, so we set dbError and continue */ $this->__IsError(); return(-1); } } else { /* Exit due to no conn id || result id */ return(-2); } } function dbGetFieldName($fieldNum) { if (isset($this->connId) && isset($this->resultId) && ($fieldNum <= $this->dbGetNumFields())) { $nameFieldVar = @ pg_fieldname($this->resultId,$fieldNum); if ($nameFieldVar) { return($nameFieldVar); } else { /* Exit on field return error, set the dbError var (not, we don't have to check for a return because this code only gets executed if isset($this->connId) */ $this->__IsError(); return(-1); } } else { /* Exit due to no connection id or result id, or $fieldnum, status -2 */ return(-2); } } function dbGetFieldNum($fieldName) { if (isset($this->connId) && isset($this->resultId) && (isset($fieldName))) { $numFieldVar = @ pg_fieldname($this->resultId,$fieldName); if ($numFieldVar) { return($numFieldVar); } else { /* Exit on field return error, set the dbError var (not, we don't have to check for a return because this code only gets executed if isset($this->connId) */ $this->__IsError(); return(-1); } } else { /* Exit due to no connection id or result id, or $fieldName, status -2 */ return(-2); } } function dbGetErrors() { if (isset($this->connId) && isset($this->dbError)) { return($this->dbError); } } function __IsError() { if (isset($this->connId)) { $errorVar = @ pg_errormessage($this->connId); if ($errorVar) { $this->dbError = $errorVar; } else { /* Exit on error return failure, instead of returning status, which would just make this a pain, lets just set this error in $dbError */ $this->dbError = "psqlObj->__IsError() : Unable to acquire error status"; } } else { /* Exit due to no connection id, status -2 */ return(-2); } } } ?>