We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to check if there is connection to the database?

I need to know if there is connection to the database if not display an error message



58.4k

Hello

You can use try catch to do this, for example

        try {

        $config = array(
        "host" => "192.168.0.11",
        "dbname" => "blog",
        "port" => 3306,
        "username" => "sigma",
        "password" => "secret"
        );

        return $connection = new \Phalcon\Db\Adapter\Pdo\Mysql($config);

        }
        catch(Exception $e) {
            //dosomething
            return false;
        }


7.6k
Accepted
answer

in controller you can use also try - catch

try { $this->db; } catch (\Exception $e) { $this->logger->critical("cannot connect to database"); return; }



81.1k

thank you very much

in controller you can use also try - catch

try { $this->db; } catch (\Exception $e) { $this->logger->critical("cannot connect to database"); return; }