Welcome Guest, Not a member yet? Register   Sign In
Store multiple DB connection strings in database...
#1

[eluser]Unknown[/eluser]
is there a solution to store multiple DB connection strings for several databases (Oracle, MSSQL, PostGres, etc) in a database table? like a virtual configuration string that connects to a specific database based on a call.

I have several databases I'll need to connect to but don't want to add each DB configuration in the database.php config file.

e.g:
Default MySQL config will keep all the connection strings

Code:
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'user';
$db['default']['password'] = 'password';
$db['default']['database'] = 'database';
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;


Now, If the data requested requires MSSQL driver, pull config from default MySQL database.

Code:
$db['mssql']['hostname'] = '$host';
$db['mssql']['username'] = '$username';
$db['mssql']['password'] = '$password';
$db['mssql']['database'] = '$database';
$db['mssql']['dbdriver'] = 'mssql';

Now, If the data requested requires Oracle driver, pull config from default MySQL database.

Code:
$tnsname = '(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = x.x.x.x)(PORT = xxxx))
(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = my_service_name)))';
$db['oci8']['hostname'] = $tnsname;
$db['oci8']['username'] = '$username';
$db['oci8']['password'] = '$password';
$db['oci8']['database'] = '$database';
$db['oci8']['dbdriver'] = 'oci8';
And so on...
#2

[eluser]CroNiX[/eluser]
Sure, but it would require more work than just storing them in the config file. Check the db user manual for connecting to multiple databases to see how to manually connect using your retrieved db values. You can just pass it a custom config array using your new values.
#3

[eluser]Unknown[/eluser]
Okay. I get the connecting to multiple DB with:
Code:
$db1 = $this->load->database('group1', TRUE);
$db2 = $this->load->database('group2', TRUE);
The question is what will the config file look like to parse data to the variables from the original post?
#4

[eluser]CroNiX[/eluser]
I've never tried to do it that way (using a named group from /config/database.php, but overriding values before connecting), so you'd have to play with it and test. You could always just create a new $config array and pass that directly to $this->load->database($config, TRUE);

Code:
$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";

$db2 = $this->load->database($config, TRUE);




Theme © iAndrew 2016 - Forum software by © MyBB