Alrighty... I think I've got all the suggestions worked through and applied in a useful way... let know what you think now 🙂
Also, this version does not incorporate the configurable hostname, username, password, and dbname. That's still being worked out (original idea ran into some troubles) so I'm using 'hard coded' values for testing purposes.
Thanks in advance! 🙂
<?php
/**
* CLASS - "db_control"
*
* Generic MySQL interface class. Allows for all basic database controls
* such as connect, query, and close. Complete list of properties and
* methods are described below.
*
*/
include_once('./include/class/error.class.php');
class db_control
{
var $db_connection; // Tracks Database Connection
var $db_name; // Name of Database to be Used
var $db_user; // Username of Database User
var $db_host; // Database Host Name
var $db_pass; // User's Database Password
var $db_result; // Storage of Query Result Set (assoc array)
var $db_last_insert_id; // Storage of Last Insert ID (assoc array)
var $db_num_of_rows; // Number of rows in result set (assoc array)
var $db_affected_rows; // Number of rows affected in last query (assoc array)
var $db_row; // Storage of Current Row Object (assoc array)
var $db_error; // Error handler object
function db_control()
{
// Set Class Connection Properties
$this->db_host = '****';
$this->db_user = '****';
$this->db_name = '****';
$this->db_pass = '****';
// Construct Error Object
$this->db_error = new error();
// Set Initial Connection
$this->db_connect();
}
function db_connect()
{
// Connect to MySQL Database
if ( !($this->db_connection = @mysql_connect($this->db_host,$this->db_user,$this->db_pass)) )
{
$this->db_error->error_code(101);
return false; // Failed Database Connection
}
// Select Database
if ( !@mysql_select_db($this->db_name,$this->db_connection) )
{
$this->db_error->error_code(102);
return false; // Failed Database Selection
}
return true; // Connection & Selection Success
}
function db_close()
{
// Close MySQL Database Connection, Clear Result and Connection Vars
if ( !@mysql_close($this->db_connection) )
{
$this->db_error->error_code(103);
return false; // Failed Connection Termination
}
else
{
unset ($this->db_connection);
unset ($this->db_result);
}
return true; // Connection Closed Successfully
}
function db_protect( $var )
{
// Open Connection to MySQL if not already present
if ( !isset($this->db_connection) )
{
$this->db_connect();
}
// Quote Variables for Safe SQL execution - Prevent Injection
if ( get_magic_quotes_gpc() )
{
// Strip Slashes
$var = stripslashes($var);
}
if ( !is_numeric($var) )
{
// Quote if not Numeric
$var = "'" . @mysql_real_escape_string($var,$this->db_connection) . "'";
}
return $var;
}
function db_query( $SQL , $result = 'default' )
{
// Open Connection to MySQL if not already present
if ( !isset($this->db_connection) )
{
$this->db_connect();
}
// Execute Given MySQL Database Query
if ( !($this->db_result[$result] = @mysql_query($SQL,$this->db_connection)) )
{
$this->db_error->error_code(104);
return false; // Failed Query Execution
}
// Set Result Properties
$this->db_num_of_rows[$result] = $this->db_number_of_rows($result);
$this->db_affected_rows[$result] = $this->db_affected_rows($result);
$this->db_last_insert_id[$result] = $this->db_last_insert_id();
return true; // Query Executed Successfully
}
function db_pull_row( $result = 'default' )
{
// Return Next Row From Result Set as an Object
if ( !($this->db_row[$result] = @mysql_fetch_object($this->db_result[$result])) )
{
return false; // No More Rows to Pull
}
return true;
}
function db_number_of_rows( $result = 'default' )
{
// Return the number of rows within the Result property (SELECT only)
if ( !($number_of_rows = @mysql_num_rows($this->db_result[$result])) )
{
return false; // Failed Return Row Count
}
return $number_of_rows;
}
function db_affected_rows( $result = 'default' )
{
// Return the number of affected rows (INSERT,UPDATE,DELETE only)
if ( !($affected_rows = @mysql_affected_rows($this->db_result[$result])) )
{
return false; // Failed Affected Row Count
}
return $affected_rows;
}
function db_last_insert_id()
{
// Return the ID number of the last INSERT query (auto_increment field)
if ( !($last_insert_id = @mysql_insert_id($this->db_connection)) )
{
return false; // Unable to Grab Last Insert ID
}
return $last_insert_id;
}
function db_status()
{
// Return a String with the Current Database Status
return @mysql_stat($this->db_connection);
}
}
?>