mirror of
https://github.com/claradex/nativegallery.git
synced 2024-12-22 16:41:04 +03:00
49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
<?php
|
|
namespace App\Services;
|
|
|
|
use \PDO;
|
|
|
|
class DB {
|
|
private static $pdoInstance = null;
|
|
private static $cache = [];
|
|
|
|
public static function connect() {
|
|
if (self::$pdoInstance === null) {
|
|
$dsn = 'mysql:host='.NGALLERY['root']['db']['host'].';dbname='.NGALLERY['root']['db']['name'].';charset=utf8mb4';
|
|
$username = NGALLERY['root']['db']['login'];
|
|
$password = NGALLERY['root']['db']['password'];
|
|
|
|
try {
|
|
self::$pdoInstance = new PDO($dsn, $username, $password);
|
|
self::$pdoInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (PDOException $ex) {
|
|
die("Connection failed: " . $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
return self::$pdoInstance;
|
|
}
|
|
|
|
public static function query($query, $params = array(), $useCache = false) {
|
|
if ($useCache && isset(self::$cache[$query])) {
|
|
return self::$cache[$query];
|
|
}
|
|
|
|
$statement = self::connect()->prepare($query);
|
|
|
|
try {
|
|
$statement->execute($params);
|
|
|
|
if (explode(' ', $query)[0] === 'SELECT' || explode(' ', $query)[0] === 'SHOW' || explode(' ', $query)[0] === 'DESCRIBE') {
|
|
$data = $statement->fetchAll(PDO::FETCH_ASSOC);
|
|
if ($useCache) {
|
|
self::$cache[$query] = $data;
|
|
}
|
|
return $data;
|
|
}
|
|
} catch (PDOException $ex) {
|
|
die("Query failed: " . $ex->getMessage());
|
|
}
|
|
}
|
|
}
|
|
?>
|