Welcome Guest, Not a member yet? Register   Sign In
Test for database connection success
#1

[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.
#2

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

[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.
#4

[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...
#5

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

[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
#7

[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);
    }

}
#8

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

Randy




Theme © iAndrew 2016 - Forum software by © MyBB