iknb/api/DBconnect.php

66 lines
1.6 KiB
PHP
Raw Normal View History

2020-07-08 11:56:33 +00:00
<?php
/**
* Created by PhpStorm.
* User: Nermin
* Date: 28. 05. 2018
* Time: 12:12
*/
class DBconnect {
2020-07-08 14:04:28 +00:00
private static $host = "localhost";
private static $user = "nermin";
private static $password = "";
private static $schema = "dialectsdb";
2020-07-08 11:56:33 +00:00
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() {
2020-07-08 14:04:28 +00:00
// 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');
}
2020-07-08 11:56:33 +00:00
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;
}
}