1

I had developed a application in codeigniter. I had a forum like section in my application. For that I would likes to create a separate database to that controller.

So that I can maintain the site and database very easily. My question is possible in codeigniter to assign a database to a single controller ?

Thanks

3 Answers 3

1

As mentioned in manix's answer you can pass the settings to the database library constructor.

However what was missed is that you must specify true as the second parameter and capture the output in a variable.

$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

Failure to do so means you can only work with one database at a time as it replaces the database connection instead of opening a new one!

Note that we are passing in the strings group_one and group_two these are database setting groups (in your config file you normally only have the default group).

You can replace the group name with an array of config options, but it is better to use the groups!

You can then run your queries as

//query against first database
$DB1->select(...

//query against second database
$DB2->select(...

and you can close one of the connections by using $DB2->close(); or $DB1->close();

More information can be found in the userguide

Sign up to request clarification or add additional context in comments.

Comments

1

You can pass the database connection to the constructor:

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";

$this->load->database($config);

Comments

0

copy and paste at database.php

//Another database connection.
$db['database2']['hostname'] = 'localhost';
$db['database2']['username'] = 'root';
$db['database2']['password'] = '';
$db['database2']['database'] = 'dbname2';
$db['database2']['dbdriver'] = 'mysql';
$db['database2']['dbprefix'] = '';
$db['database2']['pconnect'] = FALSE;
$db['database2']['db_debug'] = TRUE;
$db['database2']['cache_on'] = FALSE;
$db['database2']['cachedir'] = '';
$db['database2']['char_set'] = 'utf8';
$db['database2']['dbcollat'] = 'utf8_general_ci';
$db['database2']['swap_pre'] = '';
$db['database2']['autoinit'] = TRUE;
$db['database2']['stricton'] = FALSE;

//how to use the second database which is configured
$CI= &get_instance();
//set the second parameter to TRUE.
$this->db2 = $CI->load->database('database2', TRUE);
$qry= $this->db2->query("SELECT * FROM db2tables");
var_dump($qry->result());

For more details Please refer the below link http://quandaflow.com/two-databases-connections-codeigniter/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.