Commit 3125a1d7 authored by Tim Schoondergang's avatar Tim Schoondergang

Merge branch '8-make-fallback-server-possible' into 'master'

Resolve "make fallback server possible"

Closes #8

See merge request !18
parents bc87d468 33f149d3
{
"name": "timmit/db-mysql",
"description": "A simple DB layer in PHP for using MySQL(i)",
"version": "v2.2.3",
"version": "v2.3.0",
"keywords": ["DB", "MySQL", "MySQLi", "database", "DB layer", "database layer"],
"license": "MIT",
"homepage": "http://www.TiMMiT.nl/",
......
......@@ -21,6 +21,20 @@ namespace TiMMiT\MySQL;
*/
public static $var = array();
/**
* The config fallback vars.
*
* @var array
*/
public static $fallback = array();
/**
* The backup active boolean.
*
* @var boolean
*/
public static $fallbackActive = false;
/**
* Max tries before failure by deadlock
*
......@@ -71,13 +85,18 @@ namespace TiMMiT\MySQL;
/**
* Class constructor.
* @param array $array settings as assoc array
* @param array $configVar settings as assoc array
* @param array $fallbackVar settings as assoc array
* @return void
*/
function __construct($array) {
if (is_array($array)) {
self::$var = $array;
self::connect();
function __construct($configVar,$fallbackVar=array()) {
if (is_array($configVar)) {
self::$var = $configVar;
}
if (is_array($fallbackVar)) {
self::$fallback = $fallbackVar;
}
self::connect();
}
/**
......@@ -96,7 +115,22 @@ namespace TiMMiT\MySQL;
*/
public static function connect() {
self::addToStats(__FUNCTION__);
self::$mlink = mysqli_connect(self::$var["host"], self::$var["username"], self::$var["password"], self::$var["database"], (int)self::$var["port"]) or trigger_error("Connection Failed: kon geen verbinding maken met de database!",E_USER_ERROR);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try{
self::$mlink = mysqli_connect(self::$var["host"], self::$var["username"], self::$var["password"], self::$var["database"], (int)self::$var["port"]);
} catch(\mysqli_sql_exception $e){
if(!empty(self::$fallback)){
self::$mlink = mysqli_connect(self::$fallback["host"], self::$fallback["username"], self::$fallback["password"], self::$fallback["database"], (int)self::$fallback["port"]);
self::$fallbackActive = true;
} else {
throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}
}
mysqli_report(MYSQLI_REPORT_OFF); // back to normal reporting
if (self::$mlink) {
mysqli_set_charset(self::$mlink,self::$charset);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment