50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
|
|
class AppDB {
|
|
|
|
const TABLE_TASKS = "tasks";
|
|
const TABLE_WEATHER = "weather";
|
|
|
|
const BOOLEANDB = 1000201;
|
|
|
|
const DBVERSION = "A000001";
|
|
|
|
private static $dbh = null;
|
|
|
|
public static function getLastInsertId() {
|
|
if ( is_null(self::$dbh) ) return 0;
|
|
return self::$dbh->lastInsertId();
|
|
}
|
|
|
|
public static function getSTH($query = "") {
|
|
if ( is_null(self::$dbh) ) self::getDBH();
|
|
if ( $query == "" ) return false;
|
|
return self::$dbh->prepare($query);
|
|
}
|
|
|
|
private static function getDBH() {
|
|
if ( !is_null(self::$dbh) ) return self::$dbh;
|
|
$dbh = new PDO("mysql:host=" . DBHOST . ";dbname=" . DBNAME, DBUSER, DBPASS);
|
|
// Try to connect to the database
|
|
try {
|
|
if ( DBTYPE == "mysql" ) {
|
|
$dbh = new PDO("mysql:host=" . DBHOST . ";dbname=" . DBNAME, DBUSER, DBPASS);
|
|
} else if ( DBTYPE == "sqlite" ) {
|
|
$dbh = new PDO("sqlite:" . SQLITEDB);
|
|
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
$dbh->exec("PRAGMA foreign_keys = ON");
|
|
} else {
|
|
throw new Exception("Bad database configuration in config file!");
|
|
exit();
|
|
}
|
|
} catch (PDOException | Exception $e) {
|
|
//header('Location: error_db.php?connection=');
|
|
echo "Couldn't connect to DB!";
|
|
exit();
|
|
}
|
|
|
|
self::$dbh = $dbh;
|
|
}
|
|
|
|
}
|