From d75cda8193d7b6d4e6986f5aee75fb9d8cf47c3e Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Fri, 14 Jan 2005 06:27:00 +0000 Subject: [PATCH] Finished database abstraction layer. --- db_mysql.php | 248 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 db_mysql.php diff --git a/db_mysql.php b/db_mysql.php new file mode 100644 index 0000000..3f5b72e --- /dev/null +++ b/db_mysql.php @@ -0,0 +1,248 @@ +link_id == 0) + { + if ($pconnect) + { + $this->link_id = @mysql_pconnect($server, $user, $password); + } + else + { + $this->link_id = @mysql_connect($server, $user, $password); + } + + if ($this->link_id == false) + { + $this->error('Link-Id == false, cannot connect'); + return false; + } + + if (@mysql_select_db($this->database, $this->link_id)) + { + return true; + } + else + { + $this->error('Cannot use the database \'' . $this->database . '\''); + return false; + } + } + } + + /** + * Send a query to MySQL + * + * @param str Query string + * + * @return res Resource ID for query + */ + function query($query_str) + { + $this->query_id = @mysql_query($query_str, $this->link_id); + $this->query_str = $query_str; + $this->history[] = $this->query_str; + + if (!$this->query_id) + { + $this->error('Invalid SQL query'); + } + + return $this->query_id; + } + + /** + * Escape a string depending on character set + * + * @param str String to be escaped + * + * @return str Escaped string + */ + function escape_string($string) + { + return @mysql_real_escape_string($string, $this->link_id); + } + + /** + * Fetch the query result as an array + * + * @param res Query-ID + * + * @return array A row of the query result + */ + function fetch_array($query_id) + { + return @mysql_fetch_array($query_id, MYSQL_ASSOC); + } + + /** + * Fetch the query result as an object + * + * @param res Query-ID + * + * @return obj An object with the query result + */ + function fetch_object($query_id) + { + return @mysql_fetch_object($query_id); + } + + /** + * Free the current MySQL query result + * + * @param res Query-ID + */ + function free_result($query_id) + { + @mysql_free_result($query_id); + $this->query_id = 0; + $this->query_str = ''; + } + + /** + * Send a MySQL query and return the first row of the results + * + * @param str Query string + * @param str Result return function (in the database layer) + * + * @return mixed Results in variable formats + */ + function query_first($query_str, $callback = 'fetch_array') + { + $resource = $this->query($query_str); + $return = $this->$callback($resource); + $this->free_result($resource); + return $return; + } + + /** + * Fetch the unique ID of the record just inserted + * + * @return int MySQL insert ID + */ + function insert_id() + { + return @mysql_insert_id($this->link_id); + } + + /** + * Fetch the number of rows in the result + * + * @param res Query-ID + * + * @return int Number of MySQL rows + */ + function num_rows($query_id) + { + return @mysql_num_rows($query_id); + } + + /** + * Fetch the number of rows affected by the query + * + * @return int Number of affected rows + */ + function affected_rows($query_id) + { + return @mysql_affected_rows($this->link_id); + } + + /** + * MySQL error wrapper for ISSO::_message() + * + * @param str User-defined error message + */ + function error($message) + { + global $_isso; + + if ($this->errshow) + { + if ($this->link_id) + { + $this->errno = mysql_errno($this->link_id); + $this->errstr = mysql_error($this->link_id); + } + + $style['code'] = 'font-family: \'Courier New\', Courier, mono; font-size: 11px;'; + + $message_prepped = "
\n

"; + $message_prepped .= "\n\t» Query:\n

" . $this->query_str ."
\n
"; + $message_prepped .= "\n\t» Error Number: " . $this->errno . "\n
"; + $message_prepped .= "\n\t» Error Message: " . $this->errstr . "\n
"; + $message_prepped .= "\n\t» Additional Notes: " . $message . "\n
"; + $message_prepped .= "\n\t» File: " . $_SERVER['PHP_SELF'] . "\n"; + $message_prepped .= "\n

\n
"; + + $_isso->_message('Database Error in `' . $_isso->application . '`', $message_prepped, 3); + exit; + } + } +} + +/*=====================================================================*\ +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file -- 2.22.5