Jump to content

Recommended Posts

M2 Download Center

Download Here ( Internal )

Hey guys,

 

I want to release my old DBManager.

I wrote it for my current CMS.

 

I recoded it and now I dont need the old one anymore.

 

You can use multiple instances with setting the instance id to 2 or 3.

<?php
	/**
	 * Author: Sphinx²
	 * Descr: Database manager class
	 */
	 
	 class dbmanager {
		private $dbconnection = false;
		private $connected;
		private $logging;
		static private $instance = array();
		private $instancen_id;
		private $host;
		private $username;
		private $password;
		
		private static $querys = 0;
		private static $escapes = 0;
		
		/**
		 * Set instance id for manage log files
		 */
		public function setInstanceID($instance_id) {
			$this->instancen_id = $instance_id;
		}
		
		/**
		 * dbmanager singleton (with multi instance support for multiple connections over the db manager)
		 */
		public static function instance($instance_id=1)  {
			if ( !isset(self::$instance[$instance_id]) ) {
				self::$instance[$instance_id] = new self;
			}
			self::$instance[$instance_id]->setInstanceID($instance_id);
			return self::$instance[$instance_id];
		}
		
		/**
		 * dbmanager check instance
		 */
		public static function instanceExists($instance_id=1)  {
			if ( !isset(self::$instance[$instance_id]) ) {
				return false;
			} else {
				return self::$instance[$instance_id]->connected();
			}
			return true;
		}
		
		/**
		 * Check if instance is connected
		 */
		public function connected() {
			return $this->connected;	
		}
		
		/**
		 * Initialize the log file and other init shit
		 */
		public function init() {

		}
		
		/**
		 * Set connect details
		 */
		public function setConnection($host,$username,$password="") {
			$this->host = $host;
			$this->username = $username;
			$this->password = $password;
		}
		
		/**
		 * mysql_connect
		 */
		public function connect() {
			if(!$this->connected()) {
				$this->dbconnection = @mysql_connect($this->host,$this->username,$this->password);
				if($this->dbconnection) {
					$this->connected = true;
					return true;
				} else {
					$this->connected = false;
					return false;
				}
			} else {
				return $this->connected();	
			}
		}
		
		/**
		 * mysql_query
		 */
		public function query($strquery) {
			if(!$this->dbconnection) {
				if(!$this->connect()) {
					// Error reporting
				}
			}
			if($this->dbconnection) {
				self::$querys++;
				$query = mysql_query($strquery,$this->dbconnection);
				if($query) {
					return $query;
				} else {
					return false;
				}
			} else {
				return false;	
			}
		}
		
		/**
		 * mysql_real_escape_string
		 */	
		public function escape($strescape) {
			if(!$this->dbconnection) {
				if(!$this->connect()) {
					// Error reporting
				}
			}
			
			if($this->dbconnection) {
				//$this->escapes++;
				self::$escapes++;
				return mysql_real_escape_string($strescape,$this->dbconnection);
			} else {
				die("DBManager: Failed to escape string (Connection lost)");	
			}
		}
		
		/**
		 * mysql_fetch_object first row
		 */	
		public function fetcho($strquery) {
			if(!$this->dbconnection) {
				if(!$this->connect()) {
					// Error reporting
				}
			}
			
			if($this->dbconnection) {
				$query = mysql_query($strquery,$this->dbconnection);
				if($query) {
					$object = mysql_fetch_object($query);
				} else {
					$object = NULL;
				}
				return $object;
			}
			return NULL;
		}
		
		/**
		 * mysql_fetch_object
		 */
		public static function fetchobj($resource) {
			return mysql_fetch_object($resource);	
		}
		
		
		/*
		 * mysql_fetch_array
		 */
		public static function fetcharr($resource) {
			return mysql_fetch_array($resource);	
		}
		
		/**
		 * mysql_num_row
		 */
		public static function num_rows($resource) {
			return mysql_num_rows($resource);	
		}
		
		public function insertID() {
			return mysql_insert_id($this->dbconnection);	
		}
		
		/**
		 * mysql_fetch_assoc
		 */	
		public function fetcha($strquery) {
			if(!$this->dbconnection) {
				if(!$this->connect()) {
					// Error reporting
				}
			}
			
			if($this->dbconnection) {
				$query = mysql_query($strquery,$this->dbconnection);
				if($query) {
					$array = mysql_fetch_assoc($query);
				} else {
					$array = NULL;
				}
				return $array;
			}
			return NULL;
		}
		
		/**
		 * return query count
		 */
		public static function getQueryCount() {
			return self::$querys;	
		}
		
		/**
		 * return escape count
		 */
		public static function getEscapeCount() {
			return self::$escapes;	
		}
			
		/**
		 * shutdown
		 */	
		public function shutdown() {
			if($this->dbconnection && $this->connected) {
				$this->connected = false;
				mysql_close($this->dbconnection);	
			}
		}
	 }

?>

Method 1:

dbmanager::instance(1)->init();
dbmanager::instance(1)->setConnection("127.0.0.1","root","password");

if(dbmanager::instance(1)->connect()) {
      $ret = dbmanager::instance()->query("SELECT * FROM account 
                                           WHERE id = '".dbmanager::instance(1)->escape("some string or something else")."'");
      if($ret) {
       // Maybe fetch array or something else
       $arr = dbmanager::fetcharr($ret);
       echo $arr["login"]; 
       } else {
        // Some code here
       } 
}

dbmanager::instance(1)->shutdown(); // Kill connection

Method 2 (Its not important to use the connect function if you dont use the escape function in your query:

dbmanager::instance(1)->init();
dbmanager::instance(1)->setConnection("127.0.0.1","root","password");

$ret = dbmanager::instance()->query("SELECT * FROM account");
if($ret) {
 // Maybe fetch array or something else
 $arr = dbmanager::fetcharr($ret);
 echo $arr["login"]; 
} else {
  // Some code here
} 


dbmanager::instance(1)->shutdown(); // Kill connection

The error reporting is not included.

You can write your own reporting with logfiles or something else.

 

This is based on the mysql php extension.

To port it to mysqli is easy I think.

 

Kind regards

Sphinx

 

 

 

  • Love 1
Link to comment
  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.