CodeIgniter Forums
dinamic database connect ? - 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: dinamic database connect ? (/showthread.php?tid=88220)



dinamic database connect ? - arslanturk - 08-09-2023

How do we connect to a different database with live values from database table without group?

Code:
$db_db =
                [
                    'DSN'      => '',
                    'hostname' => 'localhost',
                    'username' => 'root',
                    'password' => '1234',
                    'database' => 'test',
                    'DBDriver' => 'MySQLi',
                    'DBPrefix' => '',
                    'pConnect' => false,
                    'DBDebug'  => true,
                    'charset'  => 'utf8',
                    'DBCollat' => 'utf8_general_ci',
                    'swapPre'  => '',
                    'encrypt'  => false,
                    'compress' => false,
                    'strictOn' => false,
                    'failover' => [],
                    'port'     => 3306,
                ];

$db1 = db_connect($db_db); //192.168.1.11
$table1 = $db1->query("select * from table_name")->getResultObject();
$db1->close();

other :

Code:
$db_db =
                [
                    'DSN'      => '',
                    'hostname' => '147.145.251.123',
                    'username' => 'root',
                    'password' => '1234',
                    'database' => 'test',
                    'DBDriver' => 'MySQLi',
                    'DBPrefix' => '',
                    'pConnect' => false,
                    'DBDebug'  => true,
                    'charset'  => 'utf8',
                    'DBCollat' => 'utf8_general_ci',
                    'swapPre'  => '',
                    'encrypt'  => false,
                    'compress' => false,
                    'strictOn' => false,
                    'failover' => [],
                    'port'     => 3306,
                ];

$db2 = db_connect($db_db);//147.241.14.....
$table2 = $db2->query("select * from table_name")->getResultObject();
$db2->close();


should it be like this ?


RE: dinamic database connect ? - JustJohnQ - 08-09-2023

https://codeigniter.com/user_guide/database/configuration.html#setting-multiple-databases