sql_connection = mysqli_connect($db_server, $db_user, $db_pass, $db_database); if (!$this->sql_connection) { trigger_error("Database connection error: " . mysqli_connect_error()); } } public static function get() { if (self::$instance == null) { self::$instance = new Database(); } return self::$instance; } public function query(string $sql, string $types = "", ...$vars): array { $result = array(); if ($types == "") { // No types were provided, preparing a statement is not necessary $db_result = mysqli_query($this->sql_connection, $sql); } else { $stmt = mysqli_stmt_init($this->sql_connection); if (!mysqli_stmt_prepare($stmt, $sql)) { trigger_error('Internal error: ' . mysqli_error($this->sql_connection)); return $result; } mysqli_stmt_bind_param($stmt, $types, ...$vars); mysqli_stmt_execute($stmt); $db_result = mysqli_stmt_get_result($stmt); mysqli_stmt_close($stmt); } if (!$db_result) { return $result; } if (mysqli_num_rows($db_result) > 0) { while ($row = mysqli_fetch_assoc($db_result)) { array_push($result, $row); } } mysqli_free_result($db_result); return $result; } /** * Returns the auto generated ID of the last query. * This function is just a wrapper for mysqli_insert_id. * In the future, it might be better to return different * values in the query function depending on the type of * SQL query. */ public function get_last_id() { return mysqli_insert_id($this->sql_connection); } }