Skip to content

Instantly share code, notes, and snippets.

@tml
Forked from anonymous/gist:1172044
Created August 25, 2011 21:38
Show Gist options
  • Save tml/1172056 to your computer and use it in GitHub Desktop.
Save tml/1172056 to your computer and use it in GitHub Desktop.
<?php
Class Mysql extends mysqli{
private $revertDb = false;
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);
var_dump($this->revertDb);
// check if we need to revert back to previous database
if($this->revertDb){
// revert back to default database
$this->select_db($this->registry->config->database->database);
// set revertDb property to false
$this->revertDb = false;
}
// return query result
return parent::query($sql);
}
// overload properties to allow for database name toggles, i.e. $this->databaseName->query
public function __get($database){
// define revertDb property
$this->revertDb = true;
// select database
$this->select_db($database);
// return self instance
return $this;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment