我有一个可以使用两个不同数据库配置文件的在线系统。我想将它们合并为一个,但我尝试的一切都没有用,因为系统的某些部分连接到一个数据库配置文件而另一个连接到另一个文件。
这是我的第一个数据库配置:
<?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);
?>
这是我的第二个:
<?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;
}
}
?>
我想将它们合并到一个文件中,以便我的系统仍在运行。