iknb/api/DBconnect.php
2020-07-08 16:04:28 +02:00

66 lines
1.6 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Nermin
* Date: 28. 05. 2018
* Time: 12:12
*/
class DBconnect {
private static $host = "localhost";
private static $user = "nermin";
private static $password = "";
private static $schema = "dialectsdb";
private static $instance = null;
private function __construct() {
}
private function __clone() {
}
/**
* Returns a PDO instance -- a connection to the database.
* The singleton instance assures that there is only one connection active
* at once (within the scope of one HTTP request)
*
* @return PDO instance
*/
public static function getInstance() {
// Check for config overrides via enviroment variables.
if (getenv('DB_HOST')) {
self::$host = getenv('DB_HOST');
}
if (getenv('DB_USER')) {
self::$user = getenv('DB_USER');
}
if (getenv('DB_PASS')) {
self::$password = getenv('DB_PASS');
}
if (getenv('DB_SCHEMA')) {
self::$schema = getenv('DB_SCHEMA');
}
if (!self::$instance) {
$config = "mysql:host=" . self::$host
. ";dbname=" . self::$schema;
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_PERSISTENT => true,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
);
self::$instance = new PDO($config, self::$user, self::$password, $options);
}
return self::$instance;
}
}