Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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



37.9k

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;
        }


3.2k
Accepted
answer

in controller you can use also try - catch

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



36.3k

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; }