Posts Tagged ‘Singleton’

PHP Database Class September 10th, 2009

Michael Hartmayer

I just recently wrote a brand new Database class to add to my PHP library. The old one was starting to get stale. This class is capable of taking advantage of the Singleton design pattern.

<?php
 
/**
 * @author Michael Hartmayer (michaelhartmayer[at]gmail.com)
 * @copyright 2009
 */
 
	class Db {
 
		static $ref;				// Database Resource
 
		private $user 	= 'root';		// Username
		private $pass 	= '';			// Password
		private $host 	= 'localhost';		// Host
		private $db 	= 'test';		// Database
 
		private $err 	= array();		// Error Log
		private $errHalt = true;		// Toggle: Halt on DB Error?
 
		static $q;				// Last Query
		static $r;				// Last Results
 
		static $singleton;
 
		public function Db() {
			return true;
		}//endMethod
 
		public function getInstance() {
			if(!self::$singleton)
				self::$singleton = new self;
 
			return self::$singleton;
		}//endMethod
 
		public function setUser($i) {
			$this->user = $i;
			return true;
		}//endMethod 
 
		public function setPass($i) {
			$this->pass = $i;
			return true;
		}//endMethod
 
		public function setHost($i) {
			$this->host = $i;
			return true;
		}//endMethod
 
		public function setDb($i) {
			$this->db = $i;
			return true;
		}//endMethod
 
		public function setAll($h,$u,$p,$d) {
			$this->host = $h;
			$this->user = $u;
			$this->pass = $p;
			$this->db = $d;
			return;
		}//endMethod
 
		public function doConnect(){
			if(!empty($this->ref))
				$this->doDisconnect();
 
			if(!$this->ref = @mysql_connect($this->host,$this->user,$this->pass))
				return $this->dbErr(mysql_error());
 
			if(!@mysql_select_db($this->db,$this->ref))			
				return $this->dbErr(mysql_error($this->ref));
 
			return true;
		}//endMethod
 
		public function doDisconnect() {
			if(empty($this->ref)) return false;
 
			mysql_close($this->ref);
			$this->ref = null;
 
			return true;
		}//endMethod
 
		public function getRef() { 
			return($this->ref);
		}//endMethod
 
		public function dbSet($q) {
			$this->q = $q;
			if($this->r=@mysql_query($this->q,$this->ref)) 
				return true;
 
			return $this->dbErr(mysql_error($this->ref));
		}//endMethod
 
		public function dbGet($q,$type='') {
 
			switch($type) {
				case 'STR':
					$resultType = MYSQL_ASSOC;
					break;
				case 'INT':
					$resultType = MYSQL_NUM;
					break;
				default:
					$resultType = MYSQL_BOTH;
					break;
			}
 
			$this->q = $q;
			if(!$this->r = mysql_query($this->q,$this->ref)) 
				return $this->dbErr(mysql_error($this->ref));
 
			$resultArr = array();
			while($makeDbArr = mysql_fetch_array($this->r,$resultType)) {
				$resultArr[] = $makeDbArr;
			}//endWhile
 
			return $resultArr;
		}//endMethod
 
		public function dbCount($table, $matchPartialQuery='') {
			$this->q = "SELECT COUNT(*) AS 'COUNT' FROM `$table` $matchPartialQuery;";
			if($this->r = $this->dbGet($this->q))
				return $this->r[0]['COUNT'];
 
			return $this->dbErr(mysql_error($this->err));
		}//endMethod
 
		private function dbErr($err) {
			if(empty($err)) return false;
 
			$this->err[] = $err;
 
			if($this->errHalt==true) {
				echo ">> System has halted on Db Error. <br />";
				echo ">> Error Log:<br /><br />";
				print_r($err);
				exit;	
			}
 
			return err;
		}//endMethod
 
		public function strClean($str) {
			return mysql_real_escape_string($str,$this->ref);
		}//endMethod
 
	}//endClass
 
?>
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • MySpace
  • Reddit
  • StumbleUpon

Continue reading...