error)
die('Failed to connect to database on index #'.$idx.".\n");
$interface->setErrorHandler(['DB', 'errorHandler']);
$interface->query('SET NAMES ?', 'utf8mb4');
if ($options['prefix'])
$interface->setIdentPrefix($options['prefix']);
// disable STRICT_TRANS_TABLES and STRICT_ALL_TABLES off. It prevents usage of implicit default values.
if ($idx == DB_AOWOW)
$interface->query("SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION'");
// disable ONLY_FULL_GROUP_BY (Allows for non-aggregated selects in a group-by query)
else
$interface->query("SET SESSION sql_mode = ''");
self::$interfaceCache[$idx] = &$interface;
self::$connectionCache[$idx] = true;
}
public static function test(array $options, ?string &$err = '') : bool
{
$defPort = ini_get('mysqli.default_port');
$port = 0;
if (strstr($options['host'], ':'))
[$options['host'], $port] = explode(':', $options['host']);
if ($link = @mysqli_connect($options['host'], $options['user'], $options['pass'], $options['db'], $port ?: $defPort))
mysqli_close($link);
else
{
$err = '['.mysqli_connect_errno().'] '.mysqli_connect_error();
return false;
}
return true;
}
public static function errorHandler($message, $data)
{
if (!error_reporting())
return;
// continue on warning, end on error
$isError = $data['code'] > 0;
// make number sensible again
$data['code'] = abs($data['code']);
$error = "DB ERROR:
\n\n
".print_r($data, true).""; echo CLI ? strip_tags($error) : $error; if ($isError) exit; } public static function logger($self, $query, $trace) { if ($trace) // actual query self::$logs[] = [substr(str_replace("\n", ' ', $query), 0, 200)]; else // the statistics { end(self::$logs); self::$logs[key(self::$logs)][] = substr(explode(';', $query)[0], 5); } } public static function getLogs() { $out = '
| Time | Query | |
|---|---|---|
| '.$i.'. | '.$t.' | '.$l.' |