Home > PHP > PHP Singleton Database Class

PHP Singleton Database Class

Here’s a newer iteration of the PHP Database class I tend to prototype with. This one is cleaner than its predecessor.

Usage:

# Config
$aSettings = array(
  'username'  => 'foo',
  'password'  => 'xxxxxx',
  'host'      => 'localhost',
  'db'        => 'my_database'
);
 
# Get Instance, Settings, and Connect
$Db = Db::getInstance();
$Db->Settings($aSettings);
$Db->Connect();
 
# Set Query
$sQuery = "INSERT INTO `table` VALUES (`foo`,`bar`) VALUES ('value', 'value')";
$Db->Set($sQuery);
 
# Get Query
$sQuery = "SELECT * FROM table";
$aResults = $Db->Get($sQuery); // Returns an Array of Rows
 
echo $aResults[0]['foo']; // First row of "foo" Column
 
# Count
$iCounted = $Db->Count('table', "WHERE `bla`='blue'"); // Returns a Number
 
# Sanitize String
$sString = "This isn't unusual";
$sCleanString = $Db->Clean($sString);

And the Class:

<?php
  class Db {
 
    private $ref;
    private $settings;
    private $is_connected;
    private $errors = array();
 
    private static $singleton;
 
    public function __construct () {
      $this->is_connected = false;
    }
 
    public static function getInstance () {
      if(!self::$singleton) self::$singleton = new self();
      return self::$singleton;
    }
 
    public function Settings ($aSettings) {
      $this->settings = $aSettings;
    }
 
    public function Connect () {
      if($this->is_connected) $this->Disconnect();
 
      $this->ref = mysql_connect(
        $this->settings['host'],
        $this->settings['username'],
        $this->settings['password'],
        true
      );
 
      if(!$this->ref) {
        array_push($this->errors, mysql_error());
        return false;
      }
 
      $bSelectDatabase = mysql_select_db(
        $this->settings['db'],
        $this->ref
      );
 
      if(!$bSelectDatabase) {
        array_push($this->errors, mysql_error());
        return false;
      }
 
      $this->is_connected = true;
      return true;
    }
 
    public function Disconnect () {
      mysql_close($this->ref);
      $this->is_connected = false;
    }
 
    public function Clean ($sString) {
      if($this->is_connected) return mysql_real_escape_string($sString);
    }
 
    public function Set ($sQuery) {
      $bSet = mysql_query($sQuery, $this->ref);
      if(!$bSet) {
        array_push($this->errors, mysql_error());
        return false;
      }
 
      return true;
    }
 
    public function Get ($sQuery) {
      $sqlResults = mysql_query($sQuery, $this->ref);
 
      if(!$sqlResults) {
        array_push($this->errors, mysql_error());
        return false;
      }
 
      $aResults = array();
      while($aRow = mysql_fetch_array($sqlResults, MYSQL_ASSOC)) {
        $aResults[] = $aRow;
      }
 
      return $aResults;
    }
 
    public function Count ($sTable, $sCondition) {
      $sQuery = "SELECT COUNT(*) AS 'COUNT' FROM `$sTable` $sCondition";
      $aResults = $this->Get($sQuery);
 
      return $aResults[0]['COUNT'];
    }
 
    public function Errors() {
      return $this->errors;
    }
 
  }
?>
Categories: PHP Tags:
  1. No comments yet.
  1. No trackbacks yet.