Connect( DBHOST, DBUSER, DBPASS, DBNAME ); } ##------------------------------------------------------------------## # connect to the database function Connect($dbHost, $dbUser, $dbPass, $dbName) { $this->_dbConn = &ADONewConnection($this->_dbType); if ( $this->_dbConn->Connect ( $dbHost, $dbUser, $dbPass, $dbName ) ) { return true; } else { # grab error if the connection fails $this->_error = $this->_dbConn->ErrorMsg(); if ( DBDEBUG == "true" ) { $this->PrintError(); } return false; } } ##------------------------------------------------------------------## # close the connection to the database function Close() { if ( isset($this->_dbConn) ) { if ( $this->_dbConn->Close() ) { return true; } else { # grab error if the connection fails $this->_error = $this->_dbConn->ErrorMsg(); if ( DBDEBUG == "true" ) { $this->PrintError(); } return false; } } } ##------------------------------------------------------------------## # handles select queries where multiple rows are expected function Select($sql) { $this->_result = $this->_dbConn->Execute($sql); if ( $this->_result ) { $this->_rowCount = $this->_result->RecordCount(); $this->_fieldCount = $this->_result->FieldCount(); $this->_rows = $this->_result->GetRows(); return true; } else { $this->_error = $this->_dbConn->ErrorMsg(); if ( DBDEBUG == "true" ) { $this->PrintError($sql); } return false; } } ##------------------------------------------------------------------## # handles select queries where only one record is expected function SelectOne($sql) { $this->_result = $this->_dbConn->Execute($sql); if ( $this->_result ) { $this->_fieldCount = $this->_result->FieldCount(); $this->_rowCount = $this->_result->RecordCount(); $this->_row = $this->_result->FetchRow(); return true; } else { $this->_error = $this->_dbConn->ErrorMsg(); if ( DBDEBUG == "true" ) { $this->PrintError($sql); } return false; } } ##------------------------------------------------------------------## # handles select queries that need to return a restricted record set function SelectLimit($sql, $rows, $offset) { $this->_result = $this->_dbConn->SelectLimit($sql, $rows, $offset); if ( $this->_result ) { $this->_rowCount = $this->_result->RecordCount(); $this->_fieldCount = $this->_result->FieldCount(); $this->_rows = $this->_result->GetRows(); return true; } else { $this->_error = $this->_dbConn->ErrorMsg(); if ( DBDEBUG == "true" ) { $this->PrintError($sql); } return false; } } ##------------------------------------------------------------------## # handles queries that will alter data function Modify($sql) { $this->_result = $this->_dbConn->Execute($sql); if ( $this->_result ) { $this->_affectedRows = $this->_dbConn->Affected_Rows(); return true; } else { $this->_error = $this->_dbConn->ErrorMsg(); if ( DBDEBUG == "true" ) { $this->PrintError($sql); } return false; } } ##------------------------------------------------------------------## # get auto_incremented ID of last insert statement function InsertId() { $this->_result = $this->_dbConn->Insert_ID(); if ( $this->_result ) { $this->_insertId = $this->_result; return $this->_insertId; } else { $this->_error = $this->_dbConn->ErrorMsg(); if ( DBDEBUG == "true" ) { $this->PrintError(); } return false; } } ##------------------------------------------------------------------## # clean up and escape strings to be inserted into database function EscapeString($string) { $string = trim($string); if ( ! is_numeric($string) ) { $string = $this->_dbConn->qstr( $string, get_magic_quotes_gpc() ); } # the ADODB function above seems to add single quotes around the # submitted string. i like to add those myself at the time of # the query, so strip them off here $string = trim($string, "'"); return $string; } ##------------------------------------------------------------------## # print an error to the screen and then exit the script function PrintError($sql = "") { $thisScript = basename($_SERVER['PHP_SELF']); echo << Database Error

There was a database error.

Script: $thisScript

SQL: $sql

Error: $this->_error

HTML; exit; return true; } ##------------------------------------------------------------------## }