-
-
Save tml/1171975 to your computer and use it in GitHub Desktop.
This file contains hidden or 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{ | |
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