这是我的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.
答案 0 :(得分:1)
$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.