Search code examples
phpredbean

How to check the connection to MySQL database with RedBean?


How do I programmatically find out if there was an error connecting to the database via R::setup() in RedBean?

The object that is returned by this:

$ret = R::setup('mysql:host=127.0.0.1;dbname=testdatabase', 'root', '');

is the same as the object that is returned by this:

$ret = R::setup('mysql:host=127.0.0.1BADBADBAD;dbname=testdatabase', 'root', '');

And when I try to access the database, it gives a PHP error and stops execution so I can't check the value of $bean.

$bean = R::findOne('uploadedfiles',' filename = "ok" ');

And so a try/catch is also not possible because RedBean does not through a proper error that can be caught.

How can I check if the connection created in R::setup() was successful or not programmatically?


Solution

  • you may try to check your connection using

    R::getDatabaseAdapter()->getDatabase()->isConnected();
    

    or in another way

    $isConnected = R::testConnection();
    

    read more about debugging in redbean docs