CODEIGNITER 1.7.2:无法根据提供的设置连接到数据库

时间:2018-01-09 20:43:36

标签: php codeigniter mysqli xampp localhost

这是我的db配置文件:

$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
);

我的确切错误信息是:

A Database Error Occurred

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

2 个答案:

答案 0 :(得分:1)

在CI 1.7中,你需要使用" port"配置为mysqli。

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

默认DB_driver.php设置为$port = ''(第43行)。

使用mysqli_driver.php时,db_connect()函数使用此空端口并引发错误。

您可以看到以下驱动程序方法的不同之处(分别位于system / database / drivers / mysql或system / database / drivers / mysqli中的文件)

mysql_driver.php:

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

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

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

}

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

答案 1 :(得分:0)

$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.