CodeIgniter Forums
[solved] How to detect database connection error? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: [solved] How to detect database connection error? (/showthread.php?tid=43408)



[solved] How to detect database connection error? - El Forum - 07-10-2011

[eluser]Treeda[/eluser]
Hi there,

simple thing, just modify your password in database config
set debug to false (well on a production site i never ever want user to see errors, sepcial not SQL related errors, so dbdebug has to be false) and call

$this->load->database();

you don't get any error, except you start to do queries .-/

It's not returning anything and it's not firing an exception.

So just to make a site bulletproof and give the user a nice and nifty error message if the database goes down or whatever, how can i detect that?

I aprpeciate any help
Treeda


[solved] How to detect database connection error? - El Forum - 07-10-2011

[eluser]Treeda[/eluser]
SOLVED :-)

sorry i was a bit too quick posting, i just found the solution myself.


for anyone who's interested in doing the same, just do at the very first beginning, your base controller or any other fitting entry point the following.

if the connection id (conn_id) is set, you've got a working database if not then... not

Code:
if ( $this->load->database('', true)->conn_id == false)
            print "error";
        else
            print "success";