我已经厌倦了CI框架,因为我甚至无法为MSSQL Server设置多个连接。我找到了一个论坛,其中描述了如何设置多个连接,但它对我来说效果不好 - mssql_query语句抛出错误链接标识符不是MSSQL-Link资源等。 我做过这样的事情:
1)设置database.php - 数据库配置文件:
$active_group = 'suburb';
$active_record = FALSE;
$db['suburb']['hostname'] = 'XXXXXXXX';
$db['suburb']['username'] = 'XXXX';
$db['suburb']['password'] = 'XXXXXXX';
$db['suburb']['database'] = 'XXXXXXX';
$db['suburb']['dbdriver'] = 'mssql';
$db['suburb']['dbprefix'] = '';
$db['suburb']['pconnect'] = TRUE;
$db['suburb']['db_debug'] = TRUE;
$db['suburb']['cache_on'] = FALSE;
$db['suburb']['cachedir'] = '';
$db['suburb']['char_set'] = 'utf8';
$db['suburb']['dbcollat'] = 'utf8_general_ci';
$db['suburb']['swap_pre'] = '';
$db['suburb']['autoinit'] = TRUE;
$db['suburb']['stricton'] = FALSE;
$active_group = 'default';
$active_record = FALSE;
$db['default']['hostname'] = 'XXXXXX';
$db['default']['username'] = 'XXXX';
$db['default']['password'] = 'XXXXXXXX';
$db['default']['database'] = 'XXXXXX';
$db['default']['dbdriver'] = 'mssql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
2)调用load dbs函数:
$this->suburbDb = $this->load->database('suburb', true);
3)创建了查询:
$res = mssql_query("
SELECT TOP 100 ID
FROM Table", $this->suburbDb);
答案 0 :(得分:3)
你应该用这样的东西初始化两个连接
$this->suburb = $this->load->database('suburb', TRUE);
$this->default = $this->load->database('default', TRUE);
$this->db; //<--- houlds the DEFAULT loaded from config file. can only be one
$query = $this->suburb->query('SELECT TOP 100 ID FROM Table');
foreach( $query->result() as $row)
{
....
}
应该是这样的。
PS。检查codeigniter指南:CodeIgniter Guide Database Connection
[编辑:没有活动记录]
在建立连接之后,mssql应该可以像这样执行:
// Send a select query to MSSQL
$query = mssql_query('SELECT TOP 100 ID FROM Table', $this->suburb );
// Check if there were any records
if (!mssql_num_rows($query)) {
echo 'No records found';
} else {
while ($row = mssql_fetch_array($query)) {
echo $row['username'], PHP_EOL;
}
}
// Free the query result
mssql_free_result($query);
答案 1 :(得分:2)
你正在混合CI和php本机mssql驱动程序。
$this->suburbDb
是CI数据库对象而非链接标识符:
$this->suburbDb->query('SELECT TOP 100 ID FROM Table');
http://codeigniter.com/user_guide/database/queries.html
或者,如果你真的需要那么
mssql_query($query, $this->suburbDb->conn_id)