,,) as in: // $db = new MyDb("msiff", "mypass", "sp"); // To create a connection to postgres: // PgDb(,,) as in: //$db = new PgDb("msiff","","sp"); // Data access is then uniform as in either of the following: // $result = $db->query("SELECT * from p"); // echo "\n"; // while($row = $result->next_row()) { // echo ""; // foreach($row as $cell) { // echo "
$cell "; // } // echo "\n"; // } // echo "
"; // echo "\n"; // for($i=0; $i < $result->num_rows(); $i++) { // $row = $result->get_row($i); // echo ""; // foreach($row as $cell) { // echo "
$cell "; // } // echo "\n"; // } // echo "
"; // You can also access the data starting at a partiuclar row, // using seek: // $result->seek(2); // echo "\n"; // while($row = $result->next_row()) { // echo ""; // foreach($row as $cell) { // echo "
$cell "; // } // echo "\n"; // } // echo "
"; // a generic result class class Result { var $result; var $count; var $cursor; function Result($x,$n) { $this->result = $x; $this->count = $n; $this->cursor = 0; } function num_rows() { return $this->count; } } // a result class tailored for mysql class MyResult extends Result { function next_row() { if ($this->cursor < $this->count) { $this->cursor++; return mysql_fetch_row($this->result); } else { return 0; } } function get_row($i) { mysql_data_seek($this->result, $i); $row = mysql_fetch_row($this->result); if ($this->cursor < $this->count) { mysql_data_seek($this->result, $this->cursor); } return $row; } function seek($i) { $this->cursor = $i; mysql_data_seek($this->result, $i); } } // a mysql database class class MyDb { var $db; function MyDb($user, $pwd, $db_name) { // setup the mysql database connection: $this->db = mysql_connect("localhost", $user, $pwd) or die("Cannot connect to database server as user $user"); mysql_select_db($db_name,$this->db) or die("Cannot select database $db_name"); } // upd is non-zero if its an update query function query($q,$upd=0) { $x = mysql_query($q, $this->db); if (!$x) { $e = mysql_error(); $n = mysql_errno(); echo "

Invlaid query:

  $q

Error: #$n

  $e
\n"; die(); } if (!$upd) { $result = new MyResult($x, mysql_num_rows($x)); } else { $result = -1; } return $result; } } // a result class tailored for postgres class PgResult extends Result { function next_row() { if ($this->cursor < $this->count) { $row = pg_fetch_row($this->result, $this->cursor); $this->cursor++; return $row; } else { return 0; } } function get_row($i) { return pg_fetch_row($this->result, $i); } function seek($i) { if ($i >= 0 && $i < $this->count) { $this->cursor = $i; } else { die ("Invalid seek"); } } } // a postgres db class class PgDb { var $db; function PgDb($user, $pwd, $db_name) { // setup the pgsql database connection: $this->db = pg_connect("host=localhost user=$user dbname=$db_name") or die("could not connect/select db"); } // upd is non-zero if its an update query function query($q, $upd=0) { $x = pg_exec($this->db, $q); if (!$x) { $e = pg_errormessage($this->db); echo "

Invlaid query:

  $q

Error:

  $e
\n"; die(); } if (!$upd) { $result = new PgResult($x, pg_numrows($x)); } else { $result = -1; } return $result; } } ?>