* Copyright (C) 1999 Portal Web Design. All rights reserved. * A class implementing database routines with support for * many types of databases. * For use with the Portal In A Bottle toolkit. * Part of the Portal In A Bottle base utilities. /******************************************************************** * function Database($db_server_type, $host, $username, $password, * $db_server_type - Database type, defaults to "mysql" * $host - Host where the database server resides * $username - Username for the database * $password - Password for the database * $database - Database name ********************************************************************/ function Database($dbserver_type = "mysql", $host = "localhost", $username = "", $password = "", $database = "") { $dbserver_type = strtolower($dbserver_type); // if (empty($host)) { fatal_error("Host required"); } fatal_error("Username required"); if ($dbserver_type == "mysql") { $this->dbclass = new Database_MySQL(); fatal_error("This database format is not supported!"); $this->dbclass->host = $host; $this->dbclass->username = $username; $this->dbclass->password = $password; $this->dbclass->database = $database; /******************************************************************** * Connects to the database ********************************************************************/ $this->dbclass->connect(); /******************************************************************** * Closes the connection to the database ********************************************************************/ $this->dbclass->disconnect(); /******************************************************************** * function set_database($db) * Sets which database to use ********************************************************************/ function set_database($db) { $this->dbclass->database = $db; /******************************************************************** * function get_database() * Returns the name of the current database. ********************************************************************/ function get_database() { return($this->dbclass->database); /******************************************************************** ********************************************************************/ // print "SQL Call = {$query}<br />\n"; $this->dbclass->query($query); /******************************************************************** * function row_exists($rowname) * $rowname - The name of the row * Returns TRUE if the specified row exists. FALSE otherwise. ********************************************************************/ function row_exists($rowname) { return $this->row_exists($rowname); /******************************************************************** * function result_array() * Returns the result in array form ********************************************************************/ function result_array() { return($this->dbclass->result_array()); /******************************************************************** * function valid_result() * Returns TRUE if the result is not NULL, FALSE otherwise. ********************************************************************/ function valid_result() { if (empty($this->dbclass->result)) { /******************************************************************** * Returns the number of rows in the previous query. ********************************************************************/ return($this->dbclass->num_rows()); /******************************************************************** ********************************************************************/ $this->dbclass->free_result(); function get_insert_id() { return $this->dbclass->get_insert_id(); /*********************************************************************/ /******************************************************************** * class Database_MySQL extends Database * MySQL database implementation ********************************************************************/ class Database_MySQL extends Database { function Database_MySQL() {} /******************************************************************** * Connects to the database ********************************************************************/ mysql_pconnect($this->host, $this->username, $this->password) or fatal_error(mysql_error()); /******************************************************************** * Closes the connection to the database ********************************************************************/ * We want a persistant connection. This would defeat the purpose. /******************************************************************** ********************************************************************/ $this->result = mysql_db_query($this->database, $query) or fatal_error(mysql_error()); /******************************************************************** * function row_exists($rowname) * $rowname - The name of the row * $key - The key to use in the COUNT() (optional) * Returns TRUE if the specified row exists. FALSE otherwise. ********************************************************************/ function row_exists($rowname, $key = "*", $misc = "") { $this->query("SELECT COUNT($key) FROM $this->database"); /******************************************************************** * function result_array() * Returns the result in an array form ********************************************************************/ function result_array() { return(mysql_fetch_array($this->result)); /******************************************************************** * Returns the number of rows in the previous query. ********************************************************************/ return(mysql_num_rows($this->result)); /******************************************************************** ********************************************************************/ //mysql_free_result($this->result); function get_insert_id() { return mysql_insert_id();