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

[eluser]pshah.mumbai[/eluser]
I have created a custom library called "Startup" which checks/loads/initializes all the basic things that I need to have in the application.

I need to do some custom handling if the database connection is not correct. The DB_driver class throws a exception and completely kills off the application. So I have set the db_debug flag to FALSE in database.php file.

Code:
$db['default']['db_debug'] = FALSE;

And in the Startup library it checks if the connection is valid.

Code:
class Startup
{
    function Startup()
    {
        $CI =& get_instance();
        if ( ! $CI->db->conn_id) // HERE LIES THE PROBLEM
            redirect("install");

The problem is $CI->db->conn_id just checks if it is able to connect to server i.e. hostname, ports are valid. It does not tell if the username/password/dbname is valid or not. Is there any $CI->db method that tells that the username/password/dbname is valid ?
#2

[eluser]pshah.mumbai[/eluser]
One ugly solution is $CI->db->query("SHOW TABLES"); If it returns NULL then there is some issue with the connection.

Smile
#3

[eluser]InsiteFX[/eluser]
If I am not mistaken the Database will not connect if the user id and password are wrong!

InsiteFX
#4

[eluser]pshah.mumbai[/eluser]
Quote:If I am not mistaken the Database will not connect if the user id and password are wrong!

That is true if $db['default']['db_debug'] = TRUE - it will throw an exception. But if $db['default']['db_debug'] = FALSE then it will silently fail. Now if I want to manually check for a valid connection then there is no way to do it.

The $this->db->conn_id will be set if it is able to open connection to database. It does not indicate a valid database/username/password.

Below is the code from database/DB_driver.php

Code:
function initialize()
    {
        // If an existing connection resource is available
        // there is no need to connect and select the database
        if (is_resource($this->conn_id) OR is_object($this->conn_id))
        {
            return TRUE;
        }
    
        // ----------------------------------------------------------------
        
        // Connect to the database and set the connection ID
        $this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();

        // No connection resource?  Throw an error
        if ( ! $this->conn_id)
        {
            log_message('error', 'Unable to connect to the database');
            
            if ($this->db_debug)
            {
                $this->display_error('db_unable_to_connect');
            }
            return FALSE;
        }

        // ----------------------------------------------------------------

        // Select the DB... assuming a database name is specified in the config file
        if ($this->database != '')
        {
            if ( ! $this->db_select())
            {
                log_message('error', 'Unable to select database: '.$this->database);
            
                if ($this->db_debug)
                {
                    $this->display_error('db_unable_to_select', $this->database);
                }
                return FALSE;            
            }
            else
            {
                // We've selected the DB. Now we set the character set
                if ( ! $this->db_set_charset($this->char_set, $this->dbcollat))
                {
                    return FALSE;
                }
        
                return TRUE;
            }
        }

        return TRUE;
    }




Theme © iAndrew 2016 - Forum software by © MyBB