Apologies for the upper case, that's just my own personal coding style. Anyway, here's the DB_INTERFACE class definition.
<?php
class DB_INTERFACE {
// This interfacing library only permits one database connection
// at a time in order to reserve system resources and processor power
// In order to maintain database efficiency, a brand spanking new connection will be created when a
// previous connection is closed, this is done through the means of assigning the connection to an array
// variable, so that when a new connection is created it will be appended to the end of the array.
// Current database connection status parameters;
private $_CONNECTION_ID; // Integer variable containing the current connection ID;
private $_CONNECTION_LINK; // Resource array variable that contains the parameters to the database connection;
private $_CONNECTION_ERROR; // String variable that contains the current error, should there be one;
// Cureent database query paramters;
private $_QUERY_SQL; // String variable containing the SQL to be executed;
public $_QUERY_QRY; // Resource variable containing the result to the query;
public $_QUERY_ROW;
// Method to free the database resource links and results and to unset the member variables and finally free memory;
public function db_free_result() {
if (isset($this->_QUERY_QRY)) {
mysql_free_result($this->_QUERY_QRY);
}
}
// Checks to see if a connection is already open;
public function db_is_conn_open() {
if ($this->_CONNECTION_LINK[$this->_CONNECTION_ID] == TRUE) {
return true;
} else {
return false;
}
}
private function db_opn_conn() {
// Open an non-existant connection, providing one isn't already open
if (empty($this->_CONNECTION_LINK[$this->_CONNECTION_ID])) {
$this->_CONNECTION_LINK[$this->_CONNECTION_ID] = mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD) or $this->_CONNECTION_ERROR = mysql_error();
if (isset($this->_CONNECTION_ERROR)) {
echo $this->_CONNECTION_ERROR;
unset($this->_CONNECTION_ERROR);
$this->db_cls_conn();
return false;
} else {
return true;
}
} else {
// Throw an error is an attempt was made to open a new connection when one is already open;
?><p>Error: Cannot open a new MySQL database connection, one already exists.</p><?php
return false;
}
}
// Close an already open connection;
private function db_cls_conn() {
if (!empty($this->_CONNECTION_LINK[$this->_CONNECTION_ID])) {
// Close an existing connection, and increment the $_CONNECTION_ID variable by one;
mysql_close($this->_CONNECTION_LINK[$this->_CONNECTION_ID]) or $this->_CONNECTION_ERROR = mysql_error();
// Output an error if there is one, else continue;
if (isset($this->_CONNECTION_ERROR)) {
echo $this->_CONNECTION_ERROR;
unset($this->_CONNECTION_ERROR);
return false;
} else {
$this->_CONNECTION_LINK[$this->_CONNECTION_ID] = NULL;
$this->_CONNECTION_ID++;
return true;
}
} else {
// Throw an error if an attempt was made to close a non-existant connection;
?><p>Error: Cannot close a non-existant MySQL database connection.</p><?php
return false;
}
}
// Open or close a database connection
// This method is the one to be called when opening or closing
// a connection to the database as it will be automatically
// determine whether or not a connection is already open, and
// thus will open or close a single connection to the database.
public function db_connect() {
if (!isset($this->_CONNECTION_ID)) {
$this->_CONNECTION_ID = 0;
}
if (!empty($this->_CONNECTION_LINK[$this->_CONNECTION_ID])) {
$this->db_cls_conn();
} else {
$this->db_opn_conn();
}
}
// Report current connection and server status;
public function db_report_conn_stat() {
if ($this->_CONNECTION_LINK[$this->_CONNECTION_ID] == TRUE) {
// If the connection is active...
?><p>Notice: A connection to the MySQL database at '<?php echo MYSQL_HOSTNAME;?>' is valid and open.<br />
Current MySQL database server system status will now be displayed below this message.</p><?php
$status = explode(" ", mysql_stat($this->_CONNECTION_LINK[$this->_CONNECTION_ID]));
for ($i = 0; $i < count($status); $i++) {
echo ($i == 0) ? $status[$i] : "<br />\n" . $status[$i];
}
$this->db_report_conn_count();
} else {
// If the connection is inactive...
?>
<p>Error: There seems to be a problem regarding the connection to the MySQL database at '<?php echo MYSQL_HOSTNAME;?>'.<br />
Please make certain that the database server is running and/or your connection parameters are correct and try again.</p>
<?php
}
}
// Report current connection count
public function db_report_conn_count() {
?><p><?php echo $this->_CONNECTION_ID;?> individual database connection(s) made to generate this page.</p><?php
}
// Fetch data from a specific table in the database;
public function _SITE_DB_INTERFACE_OBJ($_TABLE, $_QUERY) {
if ($this->db_is_conn_open()) {
// Access a database;
mysql_select_db($_TABLE) or $this->_CONNECTION_ERROR = mysql_error();
// Output an error if there is one, else continue;
if (isset($this->_CONNECTION_ERROR)) {
echo $this->_CONNECTION_ERROR;
unset($this->_CONNECTION_ERROR);
return false;
} else {
// Access a specific table and/or row in the database and then store the resource link in the object's member variable;
$this->_QUERY_SQL = $_QUERY;
$this->_QUERY_QRY = mysql_query(mysq_real_escape_string($this->_QUERY_SQL)) or $this->_CONNECTION_ERROR = mysql_error();
echo $this->_CONNECTION_ERROR;
return true;
}
}
}
public function __destruct() {
unset($this->_CONNECTION_ID, $this->_CONNECTION_LINK, $this->_CONNECTION_ERROR, $this->_QUERY_SQL, $this->_QUERY_QRY);
return true;
}
}
?>