• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
possible to assign db_name(Database.php) in a variable or any other option

#1
Hello guys
We want to connect one of too many databases as a default.
as per user requriment so we want take a database name in a variable or Global variable.

we are unable to use any variable in Database.php file

so guys help me for this problem.
Reply

#2
You can pass the database config group to the database method.

So you would need to setup a config group for each database that you need to use.

PHP Code:
$db1 = \Config\Database::connect('group_one');
$db  = \Config\Database::connect('group_two'); 

Note: Change the words “group_one” and “group_two” to the specific group names you are connecting to.

SEE:

Multiple Connections to Same Database
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
You can also pass the entire connection group to the connect method:

PHP Code:
$custom = [
            
'DSN'      => '',
            
'hostname' => 'localhost',
            
'username' => '',
            
'password' => '',
            
'database' => '',
            
'DBDriver' => 'MySQLi',
            
'DBPrefix' => '',
            
'pConnect' => false,
            
'DBDebug'  => (ENVIRONMENT !== 'production'),
            
'cacheOn'  => false,
            
'cacheDir' => '',
            
'charset'  => 'utf8',
            
'DBCollat' => 'utf8_general_ci',
            
'swapPre'  => '',
            
'encrypt'  => false,
            
'compress' => false,
            
'strictOn' => false,
            
'failover' => [],
            
'port'     => 3306,
    ];
$db = \Config\Database::connect($custom); 

One possible solution to make this a "global" is to create a new method in Config\Services.php that determines which is the correct database to connect to for this page view and return a shared instance of the database connection with the appropriate connection. Then, each time you call that method during that load it will return that same database connection.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.