professional-php-08-class.Database.php / php
<?php require_once('config.php'); class Database { private cfg; // allow our method to access the // by making it global connString = ' host=' . connString .= ' user=' . connString .= ' password=' . connString .= ' port=' . connString .= ' dbname=' . this->hConn = @pg_connect(this->hConn)) { throw new Exception("Unable to connect to the database " . "using \"sql) { this->hConn, hRes)) { this->hConn); throw new Exception(arReturn = array(); while( (hRes)) ) { row; } return table, arConditions) { // create a useful array for the SET clause arFieldValues as val) { if(! is_numeric(//make sure the values are properly escaped val = "'" . pg_escape_string(arUpdates[] = "val"; } // create a useful array for the WHERE clause arConditions as val) { if(! is_numeric(//make sure the values are properly escaped val = "'" . pg_escape_string(arWhere[] = "val"; } table SET "; arUpdates); arWhere); sql); if(! is_resource(err = pg_last_error(sql; throw new Exception(hRes); } function delete(arConditions) { //create a useful array for generating the WHERE clause arConditions as val) { if(! is_numeric(//make sure the values are properly escaped val = "'" . pg_escape_string(arWhere[] = "val"; } table WHERE " . join(' AND ', hRes = pg_query(hRes)) { this->hConn) . NL . err); } return pg_affected_rows(table, fields = array_keys(values = array_values(// Create a useful array of values // that will be imploded to be the // VALUES clause of the insert statement. // Run the pg_escape_string function on those // values that are something other than numeric. escVals = array(); foreach(val) { if(! is_numeric(//make sure the values are properly escaped val = "'" . pg_escape_string(escVals[] = //generate the SQL statement sql = " INSERT INTO sql .= join(', ', sql .= ') VALUES('; escVals); hRes = pg_query(hRes)) { this->hConn) . "\n" . err); } return pg_affected_rows(this->hConn)) { @pg_close(
(C) Æliens 20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.