• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI4: how to check DB connection?

#1
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
Reply


Messages In This Thread
CI4: how to check DB connection? - by Avega Soft - 11-20-2019, 09:25 AM
RE: CI4: how to check DB connection? - by MGatner - 11-20-2019, 09:48 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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