CodeIgniter Forums
Multiple Databases - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Multiple Databases (/showthread.php?tid=80966)



Multiple Databases - 68thorby68 - 01-09-2022

Hi,
I've created my application with CI4 and have got to the point where I need to extend my database for multiple clients. My database has 20 tables (10 less than 100 rows, 10 with 100,000's rows). I intend to create a separate schema for each client and there coud be well over 1000 clients. Is it possible to manage so many different connections from the default group, and pull the relevant parameters from a pre-populated session. i.e.:
PHP Code:
$default = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => session()->get('db_username'),
    'password' => session()->get('db_password'),
    'database' => session()->get('db_database'),
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'    => 3306,
]; 

I suppose I have 2 questions really, 1) Is this possible?, 2) If it is possible, is it safe to do this?
I would rather not create a group per client, or have to provide every key/data pair (Custom) for every database query.
Many thanks.