Select database dynamically - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6) +--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17) +--- Thread: Select database dynamically (/showthread.php?tid=1650) |
Select database dynamically - radiohead - 03-29-2015 Hello, I got the following scenery: My app receives an authenticated user from another external app. When the user comes to my app I must to set some session vars like: user_id, account, database, etc. My big problem is: Codeigniter by default initializes database settings when the library is called from autoload file. I would like to load database settings from an specific file, something like: config/database/database1.php, config/database/database2.php, etc., because will be used many databases. I'm not an expert programmer, I can do some ugly hacks with cookies or php native sessions but I think that's not right. I've tried some hooks like this (pre_controller): PHP Code: $database = $ci->session->userdata('database'); But it does not works for me. Any help or advice? Sorry for my bad english, thanks. RE: Select database dynamically - cpascu01 - 03-29-2015 Your code should work. You have to make sure that you have the string $database is defined in application\config\database.php For example, if $database = 'testdatabase', you should have an entry in database.php similar to below: PHP Code: $db['default'] = array( When you pass a variable to $ci->load->database($variable, TRUE), you're saying, find me the database connection with this name in database.php (03-29-2015, 12:55 PM)radiohead Wrote: |