Welcome Guest, Not a member yet? Register   Sign In
multiple database connections postgresql
#1

[eluser]swmcl[/eluser]
Hello,

I also am having difficulties connecting when I move from a 'default' connection group.

I am using 2.0.2 with the latest PHP5 and apache on ubuntu. My previous test run has worked and now I am trying to connect to a new database for another trial.

The controller code is thus:

Code:
// connect to DB and get data
//      $this->load->database();
        $this->load->database($db['app1'],TRUE);
        //Load database utils  , '',TRUE
        $this->load->dbutil();  
        $data['title'] = 'My test Site';
        $data['header'] = 'All of my data';
        $this->load->model('Cdbtestmodel');
        $data['query'] = $this->Cdbtestmodel->get_all_contacts();
        $this->load->view('cdbtestview', $data);

The database config is thus:

Code:
$db['app1']['hostname'] = 'localhost';
$db['app1']['username'] = 'steve';
$db['app1']['password'] = 'steve';
$db['app1']['database'] = 'codeigniter';
$db['app1']['dbdriver'] = 'postgre';
$db['app1']['dbprefix'] = '';
$db['app1']['pconnect'] = TRUE;
$db['app1']['db_debug'] = TRUE;
$db['app1']['cache_on'] = FALSE;
$db['app1']['cachedir'] = '';
$db['app1']['char_set'] = 'utf8';
$db['app1']['dbcollat'] = 'utf8_general_ci';
$db['app1']['swap_pre'] = '';
$db['app1']['autoinit'] = TRUE;
$db['app1']['stricton'] = FALSE;

$db['app2']['hostname'] = 'localhost';
$db['app2']['username'] = 'steve';
$db['app2']['password'] = 'steve';
$db['app2']['database'] = 'crud';
$db['app2']['dbdriver'] = 'postgre';
$db['app2']['dbprefix'] = '';
$db['app2']['pconnect'] = TRUE;
$db['app2']['db_debug'] = TRUE;
$db['app2']['cache_on'] = FALSE;
$db['app2']['cachedir'] = '';
$db['app2']['char_set'] = 'utf8';
$db['app2']['dbcollat'] = 'utf8_general_ci';
$db['app2']['swap_pre'] = '';
$db['app2']['autoinit'] = TRUE;
$db['app2']['stricton'] = FALSE;

It used to work when 'app1' was 'default'. The error is now, "You have specified an invalid database connection group".

Can anyone enlighten me what I need ? I feel as though I've tried a number of options including silly ones.

Rgds,
#2

[eluser]Frank Wong[/eluser]
I don't see your code that uses your database handle. I suspect that is where your problem is since you are not capturing the handle in a variable after setting the second parameter of load->database to TRUE.

Try doing something like this.
Code:
$my_db = $this->load->database('app1', TRUE);
$query = $my_db->query('my sql statement');
#3

[eluser]swmcl[/eluser]
Thanks Frank,

Previously I simply had the line now coloured in orange at the top of the controller code that says,
Code:
$this->load->database();
and was relying on an automatic default arrangement. Now I have the two different "apps" and wanted to simply use the second one without changing the style of how things are written. Perhaps I am not able to describe things clearly.

Cheers,
#4

[eluser]swmcl[/eluser]
I'll try to explain this again.

I've got Postgres setup with various databases. I thought I would be able to setup an 'environment' like it says in the Database Library / Configuration page of the online User Guide. So if I had say 5 separate databases I could setup 5 different environments. After which, I would then be able to setup 5 controller files to 5 completely separate and independent applications.

In the Guide it says that I can set a global change in effect by,
Code:
$active_group = "test";
But that would mean I can't work on separate projects simultaneously wouldn't it ?

On the Database / Connecting page of the User Guide it says I can specify a group name (the group being set out in the config.php file) like thus:
Code:
$this->load->database('group_name');
where the group name in the example of the User Guide is $config.

Am I to assume this is 'not working'? And that the User Guide is - how do we say - incorrect ?

Perhaps the best way is to connect to the database of choice by using the DSN method seen in Database Library / Connecting ? That would be a shame.

Perhaps all the MySQL users are also unable to use this CodeIgniter feature very well ?

Rgds,
#5

[eluser]Frank Wong[/eluser]
I thought you were trying to connect to multiple databases at the same time. Since you are just connection to one and you are changing
Code:
$active_group = 'desired_db_config';
everything should work with
Code:
$this->load->database();

One thing that could be happening is the pconnect setting in your config. Try setting that to false. If you are switching back and forth with database that is the same host, user, and pass, it might just an existing connection. In your case, it would connect to the wrong database.
#6

[eluser]swmcl[/eluser]
Thanks again Frank,

Is there any way I can NOT use the global changes with 'active_group' ? I'd really like to work on different things simultaneously without having to change the application/config/database.php file or any other file if possible.

I have actually gone and proven the active_group setting does actually work as intended in my scenario and with the settings you have outlined in your last post. So all I ask is whether it is possible to get around changing this setting.

Cheers,




Theme © iAndrew 2016 - Forum software by © MyBB