Im working on an Codeigniter framework and I need to switch between different databases to run queries.
According to Codeigniter 3.0.6 docs, I can use $this->db->db_select('db_name') to dynamically change the DB. But it does not seem to work at all.
I have created a sandbox like this:
$this->load->database();
$this->load->dbutil();
br('========Start========');
$dbs = $this->dbutil->list_databases();
foreach ($dbs as $db)
{
if ($db == 'information_schema')
continue;
br($db);
$this->db->db_select($db);
if ($this->db->table_exists('users'))
br('Yes');
else
br('No');
echo $this->db->last_query();
//$tables = $this->db->list_tables();
//pp($tables);
br('-----------------------');
}
br('========End========');
The result is: it prints different DB names, but the YES/NO and last_query are all the same, and it always run on the first DB.
So I create another test to manually switch DB and the result is the same.
I also try to remove the DB name in config/database.php and set $this->db->db_select('my_third_db_name') and it always run the query on this third DB.
Did I miss something in the code? or there is bug here?
Thanks
P/S: Im connecting to 1 host only, and there are many databases in this host. And the connection is work fine
rootand that would be ridiculous in a real environment