lastInsertId()); } public static function getSTH($query = "") { if ( $query == "" ) return false; if ( is_null(self::$dbh) ) self::getDBH(); return self::$dbh->prepare($query); } private static function getDBH() { if ( !is_null(self::$dbh) ) return self::$dbh; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, ]; // Try to connect to the database try { if ( DBTYPE == "mysql" ) { $dbh = new PDO("mysql:host=" . DBHOST . ";dbname=" . DBNAME . ";charset=utf8mb4", DBUSER, DBPASS, $options); } else if ( DBTYPE == "sqlite" ) { $dbh = new PDO("sqlite:" . SQLITEDB, null, null, $options); $dbh->exec("PRAGMA foreign_keys = ON"); } else { throw new Exception("Bad database configuration in config file!"); } } catch (Exception $e) { error_log("DB connection failed: " . $e->getMessage()); http_response_code(500); header("Content-Type: application/json"); echo json_encode(["error" => true, "message" => "Bad database configuration in config file!"]); exit(); } self::$dbh = $dbh; return $dbh; } }