Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class config
- {
- /**
- * @var string $config_file
- */
- /**
- * @var array $config_values;
- */
- public $config_values = array('db_local'=>array(
- 'dbname'=>'base',
- 'host'=>'localhost',
- 'user'=>'root',
- 'password'=>''
- ));
- /**
- * @var object $instance
- */
- private static $instance = null;
- /**
- * @the constructor is set to private so
- * @so nobody can create a new instance using new
- */
- private function __construct()
- {
- }
- /**
- * Return Config instance or create intitial instance
- * @access public
- * @return object
- */
- public static function getInstance()
- {
- if(is_null(self::$instance))
- {
- self::$instance = new config();
- }
- return self::$instance;
- }
- /**
- * @get a config option by key
- * @access public
- * @param string $key:The configuration setting key
- * @return string
- */
- public function getValue($key)
- {
- return self::$config_values[$key];
- }
- /**
- * @__clone
- * @access private
- */
- private function __clone()
- {
- ;
- }
- }
- // Site configuration
- class DB {
- /**
- * @var DB
- */
- protected static $instance = null;
- private $result;
- private $query;
- private $_fetch_row = false;
- public $debug = 0;
- public $queries = array();
- private function __construct() {
- self::connect();
- if (isset($_REQUEST['showqueries']) && $_REQUEST['showqueries']) {
- $this->debug = 1;
- }
- }
- /**
- * Returns instance of DB
- * @return DB
- */
- public static function getInstance() {
- if (is_null(self::$instance))
- {
- self::$instance = new DB();
- }
- return self::$instance;
- }
- private function connect() {
- $config = config::getInstance()->config_values;
- mysql_select_db(
- $config['db_local']['base'],
- mysql_connect(
- $config['db_local']['localhost'],
- $config['db_local']['root'],
- $config['db_local']['']
- )
- );
- }
- public function query($query) {
- $this->result = array();
- $this->query = trim($query);
- if ($this->debug) {
- $time_start = microtime(true);
- }
- $result = mysql_query($this->query);
- if ($this->debug) {
- $time_run = microtime(true) - $time_start;
- $this->queries[] = array('query' => $this->query, 'time' => $time_run);
- }
- if (!$result) {
- throw new Exception($this->error());
- }
- if ($this->_fetch_row)
- {
- $this->result = mysql_fetch_row($result);
- }
- elseif (preg_match('/^select /i', $this->query) || preg_match('/^show /i', $this->query))
- {
- while ($this->result[] = mysql_fetch_assoc($result)) {}
- }
- return $result;
- }
- public function fetch_all($query = '') {
- if ($query)
- {
- $this->query($query);
- }
- return $this->result;
- }
- public function fetch_row($query = '') {
- if ($query)
- {
- $this->query($query);
- }
- return $this->result[0];
- }
- public function fetch_val($query = '') {
- if ($query)
- {
- $this->_fetch_row = true;
- $this->query($query);
- $this->_fetch_row = false;
- }
- return $this->result[0];
- }
- public function result($index, $field) {
- if ($index < 0 || $index >= sizeof($this->result)) {
- throw new Exception('DB error: ' . $index . ': no such index');
- }
- return $this->result[$index][$field];
- }
- public function numrows() {
- return sizeof($this->result);
- }
- public function error() {
- return mysql_error();
- }
- public function insert_id() {
- return mysql_insert_id();
- }
- /**
- * Singleton
- */
- protected function __clone() {
- ;
- }
- }
- $db = DB::getInstance();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement