dave
dave

Reputation: 15459

CODEIGNITER 1.7.2: unable to connect to db based on provided settings

here is my db config file:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost:8080',
    'username' => 'root',
    'password' => '',
    'database' => 'some_db',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => TRUE,
    'db_debug' => FALSE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

my exact error message is this:

A Database Error Occurred

Unable to connect to your database server using the provided settings.

Upvotes: 0

Views: 1159

Answers (2)

sooraj J
sooraj J

Reputation: 1

$db_obj=$CI->load->database($config, TRUE);
if($db_obj->conn_id) {
    //do something
} else {
    echo 'Unable to connect with database with given db details.';
}
        or


You can check for the conn_id on the $db_obj

if ($db_obj->conn_id === false) {
    $config['db_debug'] = true;
    $config['hostname'] = "myMasterDatabase.com";
    $db_obj=$CI->load->database($config, TRUE);
}
This should work.

Upvotes: 0

Vickel
Vickel

Reputation: 7997

in CI 1.7 you need to use "port" config for mysqli.

$db['default']['port'] = "3306";
$db['default']['hostname'] = "localhost";

the default DB_driver.php setting is $port = '' (around line 43).

With mysqli_driver.php, the db_connect() function uses this empty port and provokes the error.

You see the difference of driver's approach below, (files located in system/database/drivers/mysql or system/database/drivers/mysqli, respectively)

mysql_driver.php:

function db_connect()
{
    if ($this->port != '')
    {
        $this->hostname .= ':'.$this->port;
    }

    return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
}

versus mysqli_driver.php:

function db_connect()
{
    if ($this->port != '')
    {
        return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
    }
    else
    {
        return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
    }

}

source: https://forum.codeigniter.com/archive/index.php?thread-12577.html

Upvotes: 2

Related Questions