-
-
Save tml/1172056 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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