php - 在 Codeigniter 中连接两个数据库

标签 php mysql database oracle codeigniter

我在 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 服务器在连接之前可用,并在服务器不可用/没有响应时显示正确的错误消息。

执行此操作的最佳方法是什么?

最佳答案

以下是确保它的代码。

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

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

关于php - 在 Codeigniter 中连接两个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32331408/

相关文章:

php - 通过将数组传递给 MySQL PHP 中的查询来动态创建 OR 条件

mysql - 由于数据类型 Bigint 的值超出范围,ID 被更改

android - 数据库中的图像不会针对每个列表项进行更改

php - 通过在php中连接字符串来创建变量名

php - 选择表 A 中需要表 B 中的信息的列的最简单方法?

php - 无法使用 Debian 连接到 SSL Postgres 外部数据库(证书权限错误)

php - API连接到mysql有时不执行查询

android - 房间数据库中的外键

mysql - 为什么根据查询的日期范围不使用created_at索引?

php - 在 laravel 中按 Spatie 事件日志的自定义属性搜索