I have an online system that works with two different database configuration files. I want to merge them into one, but everything I tried didn't work out, because some part of the system is connected to one database config file and the other one to the other file.
This is my first database config:
<?php
error_reporting(E_ALL); ini_set('display_errors', '1');
ob_start();
session_start();
//set timezone
date_default_timezone_set('Europe/London');
//database credentials
define('DBHOST','myhost');
define('DBUSER','myuser');
define('DBPASS','mypassword');
define('DBNAME','mydbname');
//application address
define('DIR','http://mywebsite.com');
define('SITEEMAIL','noreply@mywebsite.com');
try {
//create PDO connection
$db = new PDO("mysql:host=".DBHOST.";port=myport;dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
//show error
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
exit;
}
include('user.php');
$user = new User($db);
?>
And this is my second one:
<?php
ob_start();
session_start();
//set timezone
date_default_timezone_set('Europe/London');
class Database
{
private static $dbName = 'mydbname' ;
private static $dbHost = 'myhost' ;
private static $dbUsername = 'myuser';
private static $dbUserPassword = 'mypassword';
private static $cont = null;
public function __construct() {
exit('Init function is not allowed');
}
public static function connect()
{
// One connection through whole application
if ( null == self::$cont )
{
try
{
self::$cont = new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);
self::$cont->query("SET NAMES utf8");
self::$cont->query("SET CHARACTER SET utf8");
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
return self::$cont;
}
public static function disconnect()
{
self::$cont = null;
}
}
?>
I want to merge them into one file, so that my system is still working.