我想问一下,如果数据库中包含“空格”字符,为什么codeigniter无法连接到数据库,下面是连接工作的示例代码,如魅力。
$db['posko_db']['hostname'] = 'localhost';
$db['posko_db']['username'] = 'root';
$db['posko_db']['password'] = '';
$db['posko_db']['database'] = 'database'; <---- without spaces
$db['posko_db']['dbdriver'] = 'sqlsrv';
$db['posko_db']['dbprefix'] = '';
$db['posko_db']['pconnect'] = FALSE;
$db['posko_db']['db_debug'] = TRUE;
$db['posko_db']['cache_on'] = FALSE;
$db['posko_db']['cachedir'] = '';
$db['posko_db']['char_set'] = 'utf8';
$db['posko_db']['dbcollat'] = 'utf8_general_ci';
$db['posko_db']['swap_pre'] = '';
$db['posko_db']['autoinit'] = TRUE;
$db['posko_db']['stricton'] = FALSE;
这里是连接无法工作的示例代码
$db['posko_db']['hostname'] = 'localhost';
$db['posko_db']['username'] = 'root';
$db['posko_db']['password'] = '';
$db['posko_db']['database'] = 'data base'; <---- with spaces
$db['posko_db']['dbdriver'] = 'sqlsrv';
$db['posko_db']['dbprefix'] = '';
$db['posko_db']['pconnect'] = FALSE;
$db['posko_db']['db_debug'] = TRUE;
$db['posko_db']['cache_on'] = FALSE;
$db['posko_db']['cachedir'] = '';
$db['posko_db']['char_set'] = 'utf8';
$db['posko_db']['dbcollat'] = 'utf8_general_ci';
$db['posko_db']['swap_pre'] = '';
$db['posko_db']['autoinit'] = TRUE;
$db['posko_db']['stricton'] = FALSE;
我试图以多种方式使用,但仍然没有工作,任何人都可以提供帮助? 感谢。
答案 0 :(得分:0)
数据库名称,表名称或字段名称中不允许有空格,因此以下代码将永远不会起作用,因为它超过了规则。
$db['posko_db']['database'] = 'data base'; <---- with spaces -->
应该是这样的,
$db['posko_db']['database'] = 'database';
答案 1 :(得分:0)
这适用于我:更改system \ database \ drivers \ sqlsrv \ sqlsrv_driver.php
function db_connect($pooling = false)
{
// Check for a UTF-8 charset being passed as CI's default 'utf8'.
$character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
$connection = array(
'UID' => empty($this->username) ? '' : $this->username,
'PWD' => empty($this->password) ? '' : $this->password,
'ConnectionPooling' => $pooling ? 1 : 0,
'CharacterSet' => $character_set,
'ReturnDatesAsStrings' => 1
);
// If the username and password are both empty, assume this is a
// 'Windows Authentication Mode' connection.
if(empty($connection['UID']) && empty($connection['PWD'])) {
unset($connection['UID'], $connection['PWD']);
}
$conn = sqlsrv_connect($this->hostname, $connection);
sqlsrv_query($conn,'USE ['.$this->database.']');
return $conn;
}
原件:
function db_connect($pooling = false)
{
// Check for a UTF-8 charset being passed as CI's default 'utf8'.
$character_set = (0 === strcasecmp('utf8', $this->char_set)) ? 'UTF-8' : $this->char_set;
$connection = array(
'UID' => empty($this->username) ? '' : $this->username,
'PWD' => empty($this->password) ? '' : $this->password,
'Database' => $this->database,
'ConnectionPooling' => $pooling ? 1 : 0,
'CharacterSet' => $character_set,
'ReturnDatesAsStrings' => 1
);
// If the username and password are both empty, assume this is a
// 'Windows Authentication Mode' connection.
if(empty($connection['UID']) && empty($connection['PWD'])) {
unset($connection['UID'], $connection['PWD']);
}
return sqlsrv_connect($this->hostname, $connection);
}