diff options
Diffstat (limited to 'lib/database.class.php')
| -rw-r--r-- | lib/database.class.php | 83 |
1 files changed, 32 insertions, 51 deletions
diff --git a/lib/database.class.php b/lib/database.class.php index f72bdab..5733c99 100644 --- a/lib/database.class.php +++ b/lib/database.class.php @@ -1,8 +1,5 @@ <?php -# include the ADODB library for abstracting the underlying database -require(ADODBDIR . "/adodb.inc.php"); - # define a class for connecting to, extracting and modifying data from a database class Database { @@ -36,21 +33,12 @@ class Database { # connect to the database function Connect($dbHost, $dbUser, $dbPass, $dbName) { - $this->_dbConn = &ADONewConnection($this->_dbType); - if ( - $this->_dbConn->Connect - ( - $dbHost, - $dbUser, - $dbPass, - $dbName - ) - ) - { + try { + $this->_dbConn = new PDO($this->_dbType . ':host=localhost;charset=utf8;dbname=' . $dbName, $dbUser, $dbPass); return true; - } else { + } catch (PDOException $e) { # grab error if the connection fails - $this->_error = $this->_dbConn->ErrorMsg(); + $this->_error = $e->getMessage(); if ( DBDEBUG == "true" ) { $this->PrintError(); } @@ -65,16 +53,8 @@ class 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; - } + $this->_dbConn = null; + return true; } } @@ -84,15 +64,15 @@ class Database { # handles select queries where multiple rows are expected function Select($sql) { - $this->_result = $this->_dbConn->Execute($sql); + $this->_result = $this->_dbConn->query($sql); if ( $this->_result ) { - $this->_rowCount = $this->_result->RecordCount(); - $this->_fieldCount = $this->_result->FieldCount(); - $this->_rows = $this->_result->GetRows(); + $this->_rowCount = $this->_result->rowCount(); + $this->_fieldCount = $this->_result->columnCount(); + $this->_rows = $this->_result->fetchAll(); return true; } else { - $this->_error = $this->_dbConn->ErrorMsg(); + $this->_error = $this->_dbConn->errorInfo()[2]; if ( DBDEBUG == "true" ) { $this->PrintError($sql); } @@ -106,15 +86,15 @@ class Database { # handles select queries where only one record is expected function SelectOne($sql) { - $this->_result = $this->_dbConn->Execute($sql); + $this->_result = $this->_dbConn->query($sql); if ( $this->_result ) { - $this->_fieldCount = $this->_result->FieldCount(); - $this->_rowCount = $this->_result->RecordCount(); - $this->_row = $this->_result->FetchRow(); + $this->_rowCount = $this->_result->rowCount(); + $this->_fieldCount = $this->_result->columnCount(); + $this->_row = $this->_result->fetch(); return true; } else { - $this->_error = $this->_dbConn->ErrorMsg(); + $this->_error = $this->_dbConn->errorInfo()[2]; if ( DBDEBUG == "true" ) { $this->PrintError($sql); } @@ -128,15 +108,16 @@ class Database { # handles select queries that need to return a restricted record set function SelectLimit($sql, $rows, $offset) { - $this->_result = $this->_dbConn->SelectLimit($sql, $rows, $offset); + $sql .= ' LIMIT ' . $rows . ' OFFSET ' . $offset; + $this->_result = $this->_dbConn->query($sql); if ( $this->_result ) { - $this->_rowCount = $this->_result->RecordCount(); - $this->_fieldCount = $this->_result->FieldCount(); - $this->_rows = $this->_result->GetRows(); + $this->_rowCount = $this->_result->rowCount(); + $this->_fieldCount = $this->_result->columnCount(); + $this->_rows = $this->_result->fetchAll(); return true; } else { - $this->_error = $this->_dbConn->ErrorMsg(); + $this->_error = $this->_dbConn->errorInfo()[2]; if ( DBDEBUG == "true" ) { $this->PrintError($sql); } @@ -150,19 +131,19 @@ class Database { # handles queries that will alter data function Modify($sql) { - $this->_result = $this->_dbConn->Execute($sql); + $this->_result = $this->_dbConn->query($sql); if ( $this->_result ) { - $this->_affectedRows = $this->_dbConn->Affected_Rows(); + $this->_affectedRows = $this->_result->rowCount(); return true; } else { - $this->_error = $this->_dbConn->ErrorMsg(); + $this->_error = $this->_dbConn->errorInfo()[2]; if ( DBDEBUG == "true" ) { $this->PrintError($sql); } return false; } - + } ##------------------------------------------------------------------## @@ -170,13 +151,13 @@ class Database { # get auto_incremented ID of last insert statement function InsertId() { - $this->_result = $this->_dbConn->Insert_ID(); + $this->_result = $this->_dbConn->lastInsertId(); if ( $this->_result ) { $this->_insertId = $this->_result; return $this->_insertId; } else { - $this->_error = $this->_dbConn->ErrorMsg(); + $this->_error = $this->_dbConn->errorInfo()[2]; if ( DBDEBUG == "true" ) { $this->PrintError(); } @@ -193,12 +174,12 @@ class Database { $string = trim($string); if ( ! is_numeric($string) ) { - $string = $this->_dbConn->qstr( $string, get_magic_quotes_gpc() ); + $string = $this->_dbConn->quote($string); } - # 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 + # the quote() function adds 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; |
