Sign up

Already have an account? Sign in
namespace Altum; class Database { public static $database = null; public static $db = null; public static function initialize() { mysqli_report(MYSQLI_REPORT_OFF); self::$database = new \mysqli( DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME ); /* Debugging */ if(self::$database->connect_error) { die('The connection to the database failed! Check the config.php file and make sure your database connection details are correct and your server is running.'); } /* Mysql profiling */ if(MYSQL_DEBUG) { self::$database->query("set profiling_history_size=100"); self::$database->query("set profiling=1"); } self::$database->set_charset('utf8mb4'); self::initialize_helper(); return self::$database; } public static function initialize_helper() { self::$db = new \Altum\Helpers\MysqliDb(self::$database); self::$db->returnType = 'object'; } public static function get($what, $from, Array $conditions = [], $order = false, $clean = true) { $what = ($what == '*') ? '*' : '`' . implode('`, `', $what) . '`'; $from = '`' . $from . '`'; $where = []; foreach($conditions as $key => $value) { $value = ($clean) ? query_clean($value) : $value; $where[] = '`' . $key . '` = \'' . $value . '\''; } $where = implode(' AND ', $where); $order_by = ($order) ? 'ORDER BY ' . $order : null; $result = self::$database->query("SELECT {$what} FROM {$from} WHERE {$where} {$order_by}"); return ($result->num_rows) ? $result->fetch_object() : false; } public static function close() { if(!self::$database) return; if(MYSQL_DEBUG) { $result = self::$database->query("show profiles"); while($profile = $result->fetch_object()) { echo $profile->Query_ID . ' - ' . round($profile->Duration, 10) . ' s - ' . $profile->Query . '
'; } } self::$database->close(); } }