【问题标题】:Codeigniter load different database configCodeigniter 加载不同的数据库配置
【发布时间】:2016-03-05 22:19:38
【问题描述】:

在 CodeIgniter 中,如何从单独的文件而不是默认的 config\database.php 加载数据库?

假设在库下我有一个名为 db_configs 的文件夹。在每个数据库中,详细信息将存储在单独的文件中,例如。 DB_01.php、DB_02.php等

谢谢,

【问题讨论】:

  • 在加载数据库之前加载你的配置文件。有什么问题?

标签: php mysql codeigniter


【解决方案1】:

使用 CI 内置的功能要容易得多,其中可以在一个文件中定义多个数据库连接。否则就是重新发明轮子。

加载数据库时可以选择任何给定的连接集(如 database.php 中定义的)。例如,给定 DB_01 和 DB_02,您将加载它们

$this->load->database('DB_01');

$this->load->database('DB_02');

如果你同时需要两者,你可以这样做

$db1 = $this->load->database('DB_01', TRUE);
$db2 = $this->load->database('DB_02', TRUE);

但是,如果您必须有单独的文件,则有几种不同的方法。 也许最简单的方法是使用助手

application/helpers/db2_helper.php

function db2Config()
{
  return array(
    'dsn' => '',
    'hostname' => 'localhost',
     ... etc.
  );
}

在某些控制器中

$this->load->helper('db2');
$db2_settings = db2Config();
$this->load->database($db2_settings);

也可以像这样使用 Config 类来完成。

application/config/db2.php

<?php

$config['dsn'] = '';
$config['hostname'] = 'localhost';
$config['username'] = 'IAmAUser';
$config['password'] = 'mypassword';
$config['database'] = 'theDB';
$config['dbdriver'] = 'mysqli';
$config['dbprefix'] = '';
$config['pconnect'] = TRUE;
...

在某些控制器中

$this->config->load('db2', TRUE);
$db2_config = $this->config->item('db2');
$this->load->database($db2_config);

【讨论】:

  • 是的,我知道该怎么做。不幸的是,项目特定决定了另一种方法
  • 显示此错误 在非对象上调用成员函数 get()
【解决方案2】:

最好使用 Codeigniter 提供的 database.php


重要!!!!!!

如果您只需要在同一连接上使用不同的数据库,您无需创建单独的数据库配置。您可以在需要时切换到其他数据库,如下所示:

$this->db->db_select($database2_name);

如何创建多个数据库

在 Codeigniter 2 中

默认加载第一个数据库

$db['default']['hostname'] = 'localhost';
$db['db_1']['username'] = 'root';
$db['db_1']['password'] = '';
$db['db_1']['database'] = 'my_db';
$db['db_1']['dbdriver'] = 'mysql';
$db['db_1']['dbprefix'] = '';
$db['db_1']['pconnect'] = TRUE;
$db['db_1']['db_debug'] = TRUE;
$db['db_1']['cache_on'] = FALSE;
$db['db_1']['cachedir'] = '';
$db['db_1']['char_set'] = 'utf8';
$db['db_1']['dbcollat'] = 'utf8_general_ci';
$db['db_1']['swap_pre'] = '';
$db['db_1']['autoinit'] = TRUE;
$db['db_1']['stricton'] = FALSE;

加载第二个数据库

$db['db_2']['hostname'] = 'localhost';
$db['db_2']['username'] = 'root';
$db['db_2']['password'] = '';
$db['db_2']['database'] = 'my_db_2';
$db['db_2']['dbdriver'] = 'mysql';
$db['db_2']['dbprefix'] = '';
$db['db_2']['pconnect'] = TRUE;
$db['db_2']['db_debug'] = TRUE;
$db['db_2']['cache_on'] = FALSE;
$db['db_2']['cachedir'] = '';
$db['db_2']['char_set'] = 'utf8';
$db['db_2']['dbcollat'] = 'utf8_general_ci';
$db['db_2']['swap_pre'] = '';
$db['db_2']['autoinit'] = TRUE;
$db['db_2']['stricton'] = FALSE;

在 Codeigniter 3 中

加载第一个数据库

$db['db_1'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => '',
    'password' => '',
    'database' => '',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    '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['db_2'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => '',
    'password' => '',
    'database' => '',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE

如何加载数据库?

在 codeigniter 2 中

$db_1 = $this->load->database('db_1', TRUE); # load First DB
$db_2 = $this->load->database('db_2', TRUE);  # load Second DB
$query = $secondDb->select('first')->get('login');

通过将第二个参数设置为 TRUE(布尔值),函数将返回数据库对象。


如何在查询中使用这个??

在单个数据库的早期,我们在查询末尾使用此(以下)行。

$this->db->query();
$this->db->result();

但现在只是稍微改变一下

$db_1->query();
$db_1->result();

# or

$db_2->query();
$db_2->result();

【讨论】:

    【解决方案3】:

    为了在您的 CodeIgniter 项目中使用多个数据库连接,您只需要创建多个配置数组来简化使用多个数据库。

    以下是默认Codeigniter数据库配置数组的结构:

    $db['default']['hostname'] = 'localhost';
    
    $db['default']['username'] = 'root';
    
    $db['default']['password'] = '';
    
    $db['default']['database'] = 'cloudwaysdb';
    
    $db['default']['dbdriver'] = 'mysql';
    
    $db['default']['dbprefix'] = '';
    
    $db['default']['pconnect'] = TRUE;
    
    $db['default']['db_debug'] = FALSE;
    
    $db['default']['cache_on'] = FALSE;
    
    $db['default']['autoinit'] = FALSE;
    
    $db['default']['stricton'] = FALSE;
    
    $db['default']['cachedir'] = '';
    
    $db['default']['char_set'] = 'utf8';
    
    $db['default']['dbcollat'] = 'utf8_general_ci';
    
    $db['default']['swap_pre'] = '';
    

    所以为了创建另一个数据库连接,你应该创建另一个配置数组。该数组必须遵循相同的结构。这是数组的一个例子:

    $db['anotherdb']['hostname'] = 'XXX.XXX.X.XXX';
    
    $db['anotherdb']['username'] = 'another_user';
    
    $db['anotherdb']['password'] = '';
    
    $db['anotherdb']['database'] = 'anothercloudwaysdb';
    
    $db['anotherdb']['dbdriver'] = 'mysql';
    
    $db['anotherdb']['dbprefix'] = '';
    
    $db['anotherdb']['pconnect'] = TRUE;
    
    $db['anotherdb']['db_debug'] = FALSE;
    
    $db['anotherdb']['cache_on'] = FALSE;
    
    $db['anotherdb']['cachedir'] = '';
    
    $db['anotherdb']['char_set'] = 'utf8';
    
    $db['anotherdb']['dbcollat'] = 'utf8_general_ci';
    
    $db['anotherdb']['swap_pre'] = '';
    
    $db['anotherdb']['autoinit'] = FALSE;
    
    $db['anotherdb']['stricton'] = FALSE;
    

    此时,您的示例项目中有两个数据库。要连接到特定数据库,您必须指定数据库名称。下面是正确的语法:

    this->load->database(anotherdb, TRUE)

    连接数据库后,可以进行数据库操作,如下图:

    // load 'anothercloudwaysdb'
    
    $this->legacy_db = $this->load->database(anothercloudwaysdb, true);
    
    // fetch result from 'cloudwaysdb'
    
    $this->legacy_db->select ('*');
    
    $this->legacy_db->from ('cloudwaysdb');
    
    $query = $this->legacy_db->get();
    
    $result = $query->result ();
    

    现在,如果您需要使用第二个数据库,则必须将连接发送到模型中可用的变量:

    function db_calling_model_method()
    
    {
    
       $otherdb = $this->load->database('anotherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.
    
       $query = $otherdb->select('column_one, column_two')->get('table');
    
       var_dump($query);
    
    }
    

    来源:https://www.cloudways.com/blog/pass-data-between-functions-in-codeigniter/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-23
      • 2012-06-08
      • 1970-01-01
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      相关资源
      最近更新 更多