We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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



58.2k

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



80.7k

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