PHP ActiveRecord how to check if connection to database is successful?

ⅰ亾dé卋堺 提交于 2019-12-10 21:32:12

问题


How do I check if the initialisation was successful after I do this?

ActiveRecord\Config::initialize(function($cfg){
    $cfg->set_model_directory('models');
    $cfg->set_connections(array(
        'development' => "mysql://root:root@localhost/blog"
        ));  
});

And then I go onto doing something like:

$posts = Post::all();

Just before performing the above I'd like to check if the initialization was successful.

With a PDO object I could do:

if $con ? /*execute query*/ : /*failed to connect*/

How could I achieve the same assurance with ActiveRecord?


回答1:


You can either check the connection from some instance you have (lets say you have got some model instantiated as $model), like so:

$model->connection();

You can probably do it statically, by calling the connection class

\Activerecord\Connection::instance();


来源:https://stackoverflow.com/questions/17637739/php-activerecord-how-to-check-if-connection-to-database-is-successful

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!