Here's some old code from my own framework, this is mostly a very stripped down version of handling database connections and functions.
Code:
class Database {
public $Hostname;
public $Username;
public $Password;
public $DbName;
public $AffectedRows = 0;
public function Connect($Database) {
$this->_database_connection = mysql_connect( $this->Hostname,
$this->Username,
$this->Password );
if (!$this->_database_connection) {
echo "Could not connect to server: <strong>$this->Hostname</strong>.";
}
return $this->_database_connection;
}
public function SelectDatabase() {
$this->_database_connection_select = mysql_select_db($this->DbName,
$this->_database_connection);
if (!mysql_select_db($this->DbName)) {
echo "Could not open database: <strong>$this->DbName</strong>.";
}
return $this->_database_connection_select;
}
public function ProcessConnection($Database) {
$Database->Connect($Database);
$Database->SelectDatabase($Database);
}
public function MainConnection($Database) {
$Database->ProcessConnection($Database);
}
public function ChangeDatabase($DbName) {
mysql_select_db($Database, $this->ConnectionID);
$this->DbName = $DbName;
}
/**
* Executes a query on the database.
* @param string The query to be executed.
*
* @example $Database->Query($SQL);
*/
public function Query($SQL) {
$this->QueryID = mysql_query($SQL);
if (!$this->QueryID) {
echo "<strong>MySQL query failed:</strong> $SQL";
return 0;
}
$this->AffectedRows = mysql_affected_rows($this->_database_connection);
return $this->QueryID;
}
/**
* Updates records in the database.
* @param string The table with wish to change
* @param array Array of changes: field => value
* @param string Changes the condition
* @return bool
*
* @example $Database->Update( 'testTable', array('name'=>'MichaelP' ), 'ID=2' );
*/
public function Update($Table, $Changes, $Condition) {
$Update = "UPDATE " . $Table . " SET ";
foreach($Changes as $Field => $Value) {
$Update .= "`" . $Field . "`='{$Value}',";
}
$Update = substr($Update, 0, -1);
if ($Condition != '') {
$Update .= "WHERE " . $Condition;
}
$this->Query($Update);
return true;
}
/**
* Deletes records from the database.
* @param string The table to remove rows from
* @param string The condition for which rows are to be removed
* @param int The number of rows to be removed
* @return void
*
* @example $Database->Delete( 'testTable', "name='MichaelP'", 5 );
*/
public function Delete($Table, $Condition, $Limit) {
$Limit = ( $Limit == '' ) ? '' : ' LIMIT ' . $Limit;
$Delete = "DELETE FROM {$Table} WHERE {$Condition} {$LIMIT}";
$this->Query($Delete);
}
/**
* Inserts records into the database.
* @param string The table you wish to insert records into
* @param array Array data to insert: field => value
* @return bool
*
* @example $Database->Insert( 'testTable', array('name'=>'Michael' ) );
*/
public function Insert($Table, $Data) {
$Fields = "";
$Values = "";
foreach ($Data as $F => $V) {
$Fields .= "`$F`,";
$Values .= ( is_numeric( $V ) && ( intval( $V ) == $V ) ) ? $V."," : "'$V',";
}
$Fields = substr($Fields, 0, -1);
$Values = substr($Values, 0, -1);
$Insert = "INSERT INTO $Table ({$Fields}) VALUES({$Values})";
$This->Query($Insert);
return true;
}
public function Close() {
if (!mysql_close($this->_database_connection)) {
echo "Connection close failed.";
}
}
}
Which can be initialized as such:
Code:
session_start();
$Database = new blm_Database();
$Database->Hostname = $Hostname;
$Database->Username = $Username;
$Database->Password = $Password;
$Database->DbName = $DbName;
$Database->MainConnection($Database);
Include a config file with those variables defined and your good to go. This was done a few years ago, and since then the framework's database class is a bit better, handling queries more safely and checking for almost every possible outcome. I'd share that if it wasn't tied into error handling and the core factory. It's all object oriented, so to apply it to your code wouldn't be too hard but your looking at a complete rewrite and it's way to undocumented for me to want to provide help. I put some documentation into the script above if your not familiar with objects and what not.
Have fun. Keep up the work, maybe something will come of it.