Welcome Guest, Not a member yet? Register   Sign In
possible to assign db_name(Database.php) in a variable or any other option
#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.
Reply


Messages In This Thread
RE: possible to assign db_name(Database.php) in a variable or any other option - by kilishan - 09-09-2019, 06:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB