CodeIgniter Forums
CI4: how to check DB connection? - 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: CI4: how to check DB connection? (/showthread.php?tid=74889)



CI4: how to check DB connection? - Avega Soft - 11-20-2019

Hi evryone!!! I have small trouble with how to right way check database connection. In my case I do this way:

PHP Code:
public function checkDBConnection( array $DBConfig = [] ) : bool {

        try {

            $result false;

            if( !empty($DBConfig) ) {

                $custom = [

                    'DSN'      => $DBConfig['DNS'],
                    'hostname' => $DBConfig['hostname'],
                    'username' => $DBConfig['username'],
                    'password' => $DBConfig['password'],
                    'database' => $DBConfig['database'],
                    'DBDriver' => $DBConfig['dbdriver'],
                    'DBPrefix' => $DBConfig['prefix'],
                    'pConnect' => false,
                    'DBDebug'  => (ENVIRONMENT !== 'production'),
                    'cacheOn'  => false,
                    'cacheDir' => '',
                    'charset'  => 'utf8',
                    'DBCollat' => 'utf8_general_ci',
                    'swapPre'  => '',
                    'encrypt'  => false,
                    'compress' => false,
                    'strictOn' => false,
                    'failover' => [],
                    'port'     => $DBConfig['port'],
                ];

                $db = \Config\Database::connect($custom);

                if( $db->persistentConnect()->ping() ) {

                    // Connection was successful
                    $result true;
                }
            }

        } finally {

            return $result;
        }
    


But I am not sure in this way. Please need advice or comment with right descision. Thanks


RE: CI4: how to check DB connection? - MGatner - 11-20-2019

I don't know that there is a right way, but one possibility is to use \CodeIgniter\Database\Config::getConnections(), which "Returns an array of all db connections currently made."


RE: CI4: how to check DB connection? - Avega Soft - 11-20-2019

(11-20-2019, 09:48 AM)MGatner Wrote: I don't know that there is a right way, but one possibility is to use \CodeIgniter\Database\Config::getConnections(), which "Returns an array of all db connections currently made."

Thanks! I am try it with use db config


RE: CI4: how to check DB connection? - babski123 - 11-02-2022

Don't we have a better solution for this? It seems that \CodeIgniter\Database\Config::getConnections() is not returning the actual connections but the predefined connection properties. Even if you input a wrong database name in the .env or Database.php file, it is still getting returned by this function