CodeIgniter Forums
Database Connection Check - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Database Connection Check (/showthread.php?tid=78325)



Database Connection Check - alimotiwala97 - 01-03-2021

I am connecting multiple database to my software. I need to check the status of the database connection in controller. If the database is connected it should show the message connected and if it is not connected it should redirect me to baseurl or etc.
Thankyou in Advance


RE: Database Connection Check - captain-sensible - 01-04-2021

you can do this sort of thing:

$db2 = \Config\Database::connect('custom');
$t= \Config\Database::getConnections();
print_r($t);

output of print_r($t) is:

Code:
Array ( [custom] => CodeIgniter\Database\SQLite3\Connection Object ( [DBDriver] => SQLite3 [escapeChar] => ` [_random_keyword:protected] => Array ( [0] => RANDOM() ) [DSN:protected] => [port:protected] => 3306 [hostname:protected] => localhost [username:protected] => [password:protected] => [database:protected] => /var/www/htdocs/andrinaPerfectBeauty/writable/Art2 [subdriver:protected] => [DBPrefix:protected] => [pConnect:protected] => [DBDebug:protected] => 1 [cacheOn:protected] => [cacheDir:protected] => [charset:protected] => utf8 [DBCollat:protected] => utf8_general_ci [swapPre:protected] => [encrypt:protected] => [compress:protected] => [strictOn:protected] => [failover:protected] => Array ( ) [lastQuery:protected] => [connID] => [resultID] => [protectIdentifiers] => 1 [reservedIdentifiers:protected] => Array ( [0] => * ) [likeEscapeStr] => ESCAPE '%s' [likeEscapeChar] => ! [dataCache] => Array ( ) [connectTime:protected] => [connectDuration:protected] => [pretend:protected] => [transEnabled] => 1 [transStrict] => 1 [transDepth:protected] => 0 [transStatus:protected] => 1 [transFailure:protected] => [aliasedTables:protected] => Array ( ) ) )