Codeigniter加载不同的数据库配置

时间:2015-12-01 20:45:58

标签: php mysql codeigniter

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

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

谢谢,

3 个答案:

答案 0 :(得分:5)

使用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);

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

<强>应用/助手/ db2_helper.php

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

在某些控件中

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

也可以像这样使用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);

答案 1 :(得分:1)

最好的事情是使用Codeigniter提供database.php

  
    
      

重要!!!!!

             

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

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

如何创建多个数据库

在Codeigniter 2中

将第一个数据库加载为defult

$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(布尔值),该函数将返回数据库对象。

  

如何在Query ??

中使用它

在单个数据库的早期,我们在查询结束时使用此(下面)行。

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

但现在只需稍微改变一下

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

# or

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

答案 2 :(得分:0)

为了在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-&gt; load-&gt;数据库(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/