Welcome Guest, Not a member yet? Register   Sign In
Connect to more than one database in a single connection
#4

(This post was last modified: 04-01-2024, 10:35 AM by mywebmanavgat.)

PHP Code:
$this->db = \Config\Database::connect('default');
 
//Default Database = ACCOUNT_DB;
 
$querydb1 $this->db->query('select * from users')->getRow();
 
$querydb2 $this->db->query('select * from LOG_DB.dbo.adminLogs')->getRow();
 
 
print_r($querydb1);
 
print_r($querydb2);
 die(); 


The Code above works Properly, but you cannot switch to another database with Builder because builder always adds the database of the active connection itself at the beginning of the table name.


Update Post  2:
This is the result when you try to do it with buider.

PHP Code:
$builderForDb2 $this->db->table('LOG_DB.dbo.adminLogs',false,false,false);
 
$builderForDb2->where('id',1);
 
$builderResult $builderForDb2->get()->getRow();
 
print_r($builderResult);

//Tsql Result : SELECT * FROM "ACCOUNT_DB"."dbo"."LOG_DB.dbo.adminLogs
FROM "ACCOUNT_DB"."dbo"."VALLET_LOG.dbo.adminLogs 
When setting the table name $this->db->table('LOG_DB.dbo.adminLogs'); with builder, if it takes one more parameter, and according to this parameter, if it does not add the database in the active connection to the beginning of the table name, my problem is solved. I can do this by interfering with the kernel, but there must be a way to do it without interfering with the kernel. Can't we change the database name of the active database connection later?




Update Post : 3
The code below works, but if you use left join etc. features to tables in different databases in the builder, you will encounter problems again.also every time you change the database name you have to change it back, or you have to set the database name before each query.

PHP Code:
$this->db->setDatabase('LOG_DB');
 
$builderForDb2 $this->db->table('adminLogs');
 
$builderForDb2->where('id',32);
 
$builderResult $builderForDb2->get()->getRow();
 echo 
$this->db->getLastQuery();
 
print_r($builderResult);
//Tsql SELECT * from LOG_DB.dbo.adminLogs where id=32 
Reply


Messages In This Thread
RE: Connect to more than one database in a single connection - by mywebmanavgat - 04-01-2024, 06:38 AM



Theme © iAndrew 2016 - Forum software by © MyBB