Check if database connection is made

245 views
Skip to first unread message

DownDown

unread,
May 18, 2012, 7:39:51 AM5/18/12
to redbeanphp
How can i check if the connection is made?
If searched trough the internet but can't a solution...

Something like this would be handy:

$setup = R::setup('mysql:host='. $this->host .';dbname='. $this-
>database , $this->user, $this->password);

if($setup) {
// connection made
}

else {
// no valid connection
}

Rikki

unread,
Nov 14, 2012, 11:42:15 AM11/14/12
to redbe...@googlegroups.com
This doesn't properly catch incorrect host and database names... Any way around this?

I want to catch all eventualities.

Cheers!

On Saturday, 18 August 2012 20:30:26 UTC+1, Tri Nguyen wrote:
        try{
   R::setup('mysql:host=XXXX;dbname=XXXX','XXXX','XXXX'); 
                    R::$adapter->getDatabase()->connect();
   return TRUE;
        }
        catch(PDOException $e){
return FALSE;

gabor

unread,
Nov 15, 2012, 3:13:26 AM11/15/12
to redbe...@googlegroups.com

Hi,

You can connect to a database yourself and then pass the connection to R::setup().
Personally I like to catch all exceptions at top level, like this:


try{ $app->run(); }catch(Exception $e ){
 log_error($e); serveErrorPage();
}

Also make sure you turn error reporting off on production servers!
On a production server you log the errors and exceptions but you never should display them to the user.

cheers,
Gabor
Reply all
Reply to author
Forward
0 new messages