0

I have codeigniter configured to use multiple databases. For some customers, we need to query an additional database so this is a matter of:

if( isConfigured(database) )
    foo();

However, the attempts I have made have either given the same result whether or not the db is configured in databases.php, or throws an error.

I have attempted the following:

if( $this->load->database('optional') == FALSE )

Which raises the error You have specified an invalid database connection group

try( $this->load->database('optional') )
{
    foo();
}
catch(Exception $e)
{
     doNothing();
}

Which raises the same error

The documentation states that $this->load->database('optional', TRUE) returns the connection ID however I cannot check this as the code errors out before returning a value.

I was expecting to be able to check whether or not a property had been set, e.g. $this->config->item('db')['optional'] but this would be largely guesswork to determine how to access the correct property

I have also looked into the dbutil class but this only applies to databases which have already established a connection.

Hugo Buff
  • 411
  • 5
  • 14
  • maybe this will help? http://stackoverflow.com/questions/19309468/you-have-specified-an-invalid-database-connection-group-codeigniter-error http://stackoverflow.com/questions/9543855/codeigniter-multidatabse-connect-error – qwertzman Feb 22 '16 at 12:47
  • I already have 2 databases configured and working, but a 3rd is optional – Hugo Buff Feb 23 '16 at 15:11

1 Answers1

0
$db['db1']['hostname'] = 'localhost';
$db['db1']['username'] = 'root';
$db['db1']['password'] = '';
$db['db1']['database'] = 'mydatabase';
$db['db1']['dbdriver'] = 'mysql';
$db['db1']['dbprefix'] = '';

$this->load->database('db1',TRUE);
safin chacko
  • 1,345
  • 1
  • 11
  • 18
  • `$this->load->database('optional', TRUE)` returns the connection ID however I cannot check this as the code errors out before returning a value. – Hugo Buff Feb 22 '16 at 13:01