【问题标题】:Connecting two databases in Codeigniter在 Codeigniter 中连接两个数据库
【发布时间】:2015-11-26 16:16:29
【问题描述】:

我在 codeigniter 中连接两个数据库。我的database.php配置如下。

    $db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'dvrs',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

$db['orcl_db'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'mvrs',
    'password' => 'mvrs',
    'database' => 'MVRS',
    'dbdriver' => 'oci8',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

现在我正在自动加载默认数据库,并使用

按需加载相应模型中的 orcl_db
$this->orclDB = $this->database->load("orcl_db", TRUE);

我正在连接到两个数据库并成功运行查询。

我需要在连接之前确保 oracle 服务器可用,并在服务器不可用/没有响应时显示正确的错误消息。

最好的方法是什么?

【问题讨论】:

  • $db['default']['default'] = array(...); $db['default']['orcl_db'] = array(...);像这样
  • 数据库使用正常。由于我成功连接到两个数据库,因此无需更改配置文件。我只需要在连接之前确保oracle服务器可用。
  • 我可以知道您在哪里(我的意思是在哪个文件中)加载 oracle 服务器?
  • 我正在将它加载到模型的构造函数中

标签: php mysql database oracle codeigniter


【解决方案1】:

以下是确保它的代码。

$this->orclDB = $this->load->database('orcl_db', TRUE);

        if (!$this->orclDB ->initialize()) {
            $response["status"] = false;
            $response["message"] = "Oracle DB is not available.";
        }

【讨论】:

    【解决方案2】:

    为了关闭数据库调试,当数据库连接失败时会抛出致命错误,在你的数据库配置文件中使用$db['orcl_db']['db_debug'] = FALSE;。然后你可以检查数据库是否像这样加载:

    if ( $this->load->database('orcl_db') === FALSE )
    {
       // do whatever you think is appropriate, but do not panick
    }
    

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 2013-05-09
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多