Welcome Guest, Not a member yet? Register   Sign In
How to load databases dynamically in codeigniter

(This post was last modified: 03-15-2023, 11:54 PM by kenjis. Edit Reason: Remove BB tags. )

I'm using Codeigniter 3.X in my application and I want to use different databases for each user dynamically. Currently, the main database is called main_db, but if a user with user id 1 logs in, then I want to use db_1 and so on. While Codeigniter does allow for the use of multiple databases, I want to be able to load them dynamically. Is there a way to achieve this?
Here's my database.php configuration file. In my model construct, I load the database using:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'username',
'password' => 'password',
'database' => 'main_db',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE

Messages In This Thread
How to load databases dynamically in codeigniter - by BobbyTanomrfp - 03-15-2023, 11:46 PM

Theme © iAndrew 2016 - Forum software by © MyBB