MikeSnead
05-25-2005, 10:29 PM
Been working on a new project that I want to make more re-usable and modular. Figured OOP was the natural way to go and that a database class would be a definite plus. I know these have been done thousands of times, but if you all would like to see how mine is currently set up here it is. I'd love to hear what you guys think or suggest I do differently. It is still early in development :)
It extends an error class, but you shouldn't need to see that... to get what's going on with it :)
<?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 extends error
{
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
var $db_row; // Storage of Current Row from Result Set
function db_control()
{
// Set Class Properties
$this->db_host = '*******';
$this->db_user = '*****';
$this->db_name = '*****';
$this->db_pass = '*****';
// 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->error_code(101);
return false; // Failed Database Connection
}
// Select Database
if ( !@mysql_select_db($this->db_name,$this->db_connection) )
{
$this->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->error_code(103);
return false; // Failed Connection Termination
}
else
{
unset ($this->db_connection);
unset ($this->db_result);
}
return true; // Connection Closed Successfully
}
function db_query( $SQL )
{
// Execute Given MySQL Database Query
if ( !($this->db_result = @mysql_query($SQL,$this->db_connection)) )
{
$this->error_code(104);
return false; // Failed Query Execution
}
return true; // Query Executed Successfully
}
function db_pull_row()
{
// Return Next Row From Result Set as Assoc. Array
if ( !($this->db_row = @mysql_fetch_object($this->db_result)) )
{
return false; // No More Rows to Pull
}
return true;
}
function db_number_of_rows()
{
// Return the number of rows within the Result property (SELECT only)
if ( !($number_of_rows = @mysql_num_rows($this->db_result)) )
{
return false; // Failed Return Row Count
}
return $number_of_rows;
}
function db_affected_rows()
{
// Return the number of affected rows (INSERT,UPDATE,DELETE only)
if ( !($affected_rows = @mysql_affected_rows($this->db_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)) )
{
$this->error_code(105);
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);
}
}
?>
It extends an error class, but you shouldn't need to see that... to get what's going on with it :)
<?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 extends error
{
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
var $db_row; // Storage of Current Row from Result Set
function db_control()
{
// Set Class Properties
$this->db_host = '*******';
$this->db_user = '*****';
$this->db_name = '*****';
$this->db_pass = '*****';
// 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->error_code(101);
return false; // Failed Database Connection
}
// Select Database
if ( !@mysql_select_db($this->db_name,$this->db_connection) )
{
$this->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->error_code(103);
return false; // Failed Connection Termination
}
else
{
unset ($this->db_connection);
unset ($this->db_result);
}
return true; // Connection Closed Successfully
}
function db_query( $SQL )
{
// Execute Given MySQL Database Query
if ( !($this->db_result = @mysql_query($SQL,$this->db_connection)) )
{
$this->error_code(104);
return false; // Failed Query Execution
}
return true; // Query Executed Successfully
}
function db_pull_row()
{
// Return Next Row From Result Set as Assoc. Array
if ( !($this->db_row = @mysql_fetch_object($this->db_result)) )
{
return false; // No More Rows to Pull
}
return true;
}
function db_number_of_rows()
{
// Return the number of rows within the Result property (SELECT only)
if ( !($number_of_rows = @mysql_num_rows($this->db_result)) )
{
return false; // Failed Return Row Count
}
return $number_of_rows;
}
function db_affected_rows()
{
// Return the number of affected rows (INSERT,UPDATE,DELETE only)
if ( !($affected_rows = @mysql_affected_rows($this->db_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)) )
{
$this->error_code(105);
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);
}
}
?>