Welcome Guest, Not a member yet? Register   Sign In
multiple db error
#1

[eluser]aligator[/eluser]
Hello, I want to use 2 databases in a codeigniter project. i read the documentation for configuring the app to recognize both db: http://ellislab.com/codeigniter/user-gui...cting.html
This is my config/database.php file:
Code:
/* Principals Database */
$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "-";
$db['default']['password'] = "-";
$db['default']['database'] = "first";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

/* Information Database */
$active_group = "second_db";
$active_record = FALSE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "-";
$db['default']['password'] = "-";
$db['default']['database'] = "second";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

In my first controller, the main one i need to use the default db. I understood that i don't have to declare the db i want to use, the "default" group one will be used. is this correct?

In another controller, after i passed the first one(which does a login logic) i want to use another db, the second one. so in my model (where i extract data) i load the second db:
Code:
$DB2 = $this->load->database('second_db', TRUE);
...
Code:
$query = $this->$DB2->get('tablename');..

An then when i want to try everything, so start with the login and so on i get this error: "An Error Was Encountered
You have specified an invalid database connection group." now i am confusedBig Grin i checked to see if i wrote the group wrong for the second db but i didn't. i tried even to place on "TRUE" in database.php for the second db, and no better result. I left it on FALSE thinking that this way the 2 db are not considered active on the same time, and i choose when the second one is active. does this make sense to you?
Did u had this problem? i know is from my code, i can't figure out from where.
Thank you for your time. :red:
#2

[eluser]aligator[/eluser]
i changed
Code:
$group = "second_db";
$record = FALSE;
this in the database.php and i didn't got that error anymore.
So by this i should understand that all new databases should be declared like this(at the begining)?
#3

[eluser]techgnome[/eluser]
Oi! There's a couple things wrong with that....

First, the name of the second group of db configuration needs a different name. By using "default" you just over wrote the settings for the default group. Second.... the Active_Group setting should point to the group you want to use as your DEFAULT database connection.

Code:
/* Principals Database */
$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "-";
$db['default']['password'] = "-";
$db['default']['database'] = "first";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

/* Information Database */
//$active_group = "second_db";
//$active_record = FALSE;

$db['second_db']['hostname'] = "localhost";
$db['second_db']['username'] = "-";
$db['second_db']['password'] = "-";
$db['second_db']['database'] = "second";
$db['second_db']['dbdriver'] = "mysql";
$db['second_db']['dbprefix'] = "";
$db['second_db']['pconnect'] = TRUE;
$db['second_db']['db_debug'] = TRUE;
$db['second_db']['cache_on'] = FALSE;
$db['second_db']['cachedir'] = "";
$db['second_db']['char_set'] = "utf8";
$db['second_db']['dbcollat'] = "utf8_general_ci";
NOW, you can load the second_db using the code you posted above. Any time you connect to a new database, you need to define a new group for that connection. The reason it failed for you is because you didn't define a group called "second_db" ... so there was no way for CI to load it.
For the record, I have three in mine - 1 default and 2 alternates - as long as you give them different names for hte group, you have have as many as you want.

-tg
#4

[eluser]aligator[/eluser]
thank you. i will try it as you said.




Theme © iAndrew 2016 - Forum software by © MyBB