• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple database connections that are user defined

#1
[eluser]getSurreal[/eluser]
I'm looking for some design advice to my application. My app connects to another applications database to run reports from it. There can be multiple instances of that application and I want it to be user selectable which one they are connecting to and I want a admin interface to define the different databases to connect to. I don't want the user to have to edit the php file to define all the database connections.

My thoughts are the best solution is to store the database connection settings in my app's database and then write something that queries that and generates the standard codeigniter database connection settings for each user defined database connection (although I'm not sure how to write such code just yet)

Thoughts and suggestions?

Thanks.

#2
[eluser]whitey5759[/eluser]
Agreed! That is the exact approach I have used in previous web applications I have built. You would just create a Model for that new table, and then when you need to connect to those databases, you just load that Model, query it, and then use the details you get back to open a new database connection.

#3
[eluser]getSurreal[/eluser]
I'm a little stuck on this.

I have my SQL connection info stored in a table. I have a model that retrieves this information, but I'm having difficutly passing it back to the controller to work with it. Ideally what I'd like to do is just extend the same settings that are in config/database.php

$db['db2']['hostname'] = "mssqlserver";
$db['db2']['username'] = "vmr";
$db['db2']['password'] = "vmr";
$db['db2']['database'] = "vmr";
$db['db2']['dbdriver'] = "mssql";
$db['db2']['dbprefix'] = "";
$db['db2']['pconnect'] = TRUE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = "";
$db['db2']['char_set'] = "utf8";
$db['db2']['dbcollat'] = "utf8_general_ci";
$db['db2']['port'] = "1433";

I could have any number of additional database connections so I need to loop through them. How can I put a variable in my first array position so it can be assigned by the query? And how can I make these new connection settings available globally in my application the same as if they were originally placed in the config/database.php file?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.