Php > Databases sample source codes
This is a database wrapper for PostgreSQL, but can be simply modified
This is a database wrapper for PostgreSQL, but can be simply modified for any other database type. 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); } }; } ?>
Php Codes
Algorithms
Arrays
Authentication
Calendar
Code Snippets
Programs
Content Manage
Contest Related
Cookies
Credit Cards
DBase Related
Databases
Date Time
Directories
E-Mail
Errors
File
File System
Forms
Handling
Graphics
HTML and PHP
Informix
Ingres
InterBase
LDAP
Look and Feel
Miscellaneous
MySQL
Other
PHP Classes
Searching
Navigation
Statistics
Strings
User Manage