Files
FE_Charts/server/database.php
dario-cfpt 068158ede9 server: add Dockerfile and environment variables
The Dockerfile (and docker-compose) simplify the server's deployment.
The environment variables avoid secrets in the code.
2024-10-02 10:24:59 +02:00

38 lines
887 B
PHP

<?php
// Singleton to connect db.
class database
{
// Hold the class instance.
private static $instance = null;
private $conn;
// The db connection is established in the private constructor.
private function __construct()
{
try {
$this->conn = new PDO(
'mysql:host=' . getenv('MYSQL_HOST') . ';dbname=' . getenv('MYSQL_DATABASE') . ';port=' . getenv('MYSQL_PORT'),
getenv('MYSQL_USER'),
getenv('MYSQL_PASSWORD')
);
} catch (PDOException $e) {
die('Connection failed: ' . $e->getMessage());
}
}
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new database();
}
return self::$instance;
}
public function getConnection()
{
return $this-> conn;
}
}