duankan6894 2015-11-10 10:33
浏览 61

如何将两个不同的mySQL数据库配置合并为一个?

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?