CodeIgniter Forums
Test for database connection success - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Test for database connection success (/showthread.php?tid=10495)



Test for database connection success - El Forum - 08-01-2008

[eluser]Aquillyne[/eluser]
I want to connect to my database with $this->load->database(), but I want to be able to control what happens if it fails (rather than it throwing up an error).

A try/catch block doesn't seem to work. The problem appears to be that CI has custom error handling here.


Test for database connection success - El Forum - 08-02-2008

[eluser]Yash[/eluser]
Just apply correct parameters I mean username,password and hostname forget everything CI handle it without any error.


Test for database connection success - El Forum - 08-02-2008

[eluser]Aquillyne[/eluser]
I want to catch the situation when the given database doesn't yet exist, so I can create it.

If you try to connect to a non-existent database with CI though, it throws up a big error. I want to catch that error but I can't work out how to.


Test for database connection success - El Forum - 08-02-2008

[eluser]Matthieu Fauveau[/eluser]
I'm also interested in a solution for this. Let's say your mysql server is frozen/not working, I don't want the user to see a random CI error message. I'd like to be able to add a function that display a custom error (also for being able to send me a message to inform me about the problem).

Sure I can hack CI Core but we should have a better method...


Test for database connection success - El Forum - 08-02-2008

[eluser]Aquillyne[/eluser]
Quite so, quite so. Missing feature?


Test for database connection success - El Forum - 08-02-2008

[eluser]Randy Casburn[/eluser]
Something like this should work. Override the mysql_driver class with this. You'll likely need to do the same with the persistent connect method as well.

Use the "Catch" to do what ever you want to do with the failed exception.

Code:
class MY_DB_mysql_driver extends CI_DB {
    /**
     * Non-persistent database connection
     *
     * @access    private called by the base class
     * @return    resource
     */    
    function db_connect()
    {
        try
        {
            return my_db_connect();          
        }
        catch(Exception $e)
        {
            return 'Caught exception: ',  $e->getMessage(), "\n";
        }
    }        

    function my_db_connect()
    {
        $conn_id = @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
        
        if (! $conn_id )
        {
            throw new Exception('DB Connection Failed.');
        }
        else
        {
            return $conn_id;
        }
        return
    }
}

Randy


Test for database connection success - El Forum - 11-02-2008

[eluser]Pachat[/eluser]
[quote author="Matthieu Fauveau" date="1217739582"]Sure I can hack CI Core but we should have a better method...[/quote]

In order to take hand on the CI Exceptions system, you may override the CI_Exceptions class :
create the file application/librairies/MY_Exceptions.php with
Code:
class MY_Exceptions extends CI_Exceptions {

    function MY_Exceptions()
    {
        parent::CI_Exceptions();
    }
      
    function show_404($page = '')
    {    
        parent::show_404($page);
    }
      
    function show_error($heading, $message, $template = 'error_general')
    {
        //Do whatever you want from $message analysis
        //and | or
        //return parent::show_error($heading, $message, $template);
    }

}



Test for database connection success - El Forum - 11-02-2008

[eluser]Randy Casburn[/eluser]
After re-reading these posts, yours is a much better solution...thanks Pachat!

Randy