Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tml
Forked from anonymous/gist:1171965
Created August 25, 2011 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tml/1171975 to your computer and use it in GitHub Desktop.
Save tml/1171975 to your computer and use it in GitHub Desktop.
<?php
Class Mysql extends mysqli{
public function __construct($registry){
// define registry property
$this->registry = $registry;
// initialize parent constructor
parent::__construct($this->registry->config->database->hostname, $this->registry->config->database->username, $this->registry->config->database->password, $this->registry->config->database->database);
}
public function query(){
// define $arguments
$arguments = func_get_args();
// define $argumentCount
$argumentCount = count($arguments);
switch($argumentCount){
case 1:
$sql = $this->escape_string($arguments[0]);
break;
default:
$sql = vsprintf($arguments[0], array_map(array($this, 'escape_string'), array_slice($arguments, 1)));
break;
}
// log queries
$this->registry->logger->log($sql);
// return query result
$result = parent::query($sql);
if (isset($this->previousDB)) {
$this->select_db($this->previousDB);
unset($this->previousDB);
}
return $result;
}
// overload properties to allow for database name toggles, i.e. $this->databaseName->query
public function __get($database){
// select database
$this->select_db($database);
// record that we changed it
$this->previousDB = $this->registry->config->database->database;
// return self instance
return $this;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment