From d895b852a6e160496ffc760d46d3719a3d62ff86 Mon Sep 17 00:00:00 2001 From: Nathan Kinkade Date: Sun, 3 Feb 2008 23:23:24 +0000 Subject: Initial checkin of nutridb.org and basic subversion directory structure --- lib/database.class.php | 239 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 lib/database.class.php (limited to 'lib/database.class.php') diff --git a/lib/database.class.php b/lib/database.class.php new file mode 100644 index 0000000..f72bdab --- /dev/null +++ b/lib/database.class.php @@ -0,0 +1,239 @@ +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; + + } + + ##------------------------------------------------------------------## + +} -- cgit v1.2.3