CodeIgniter set multiple connections for mssql dbs?
I am so sick with the CI framework already, because I can't even set multiple connection for MSSQL Server. I've found a forum where had been described how to set multiple connection, but it didn't work well for me - the mssql_query statement throws an error that the link identifier is not the MSSQL-Link resource etc. I've done something like this:
1) set the database.php - database configuration file:
$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']['dbdri开发者_运维问答ver'] = '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) call the load dbs function:
$this->suburbDb = $this->load->database('suburb', true);
3) created the query:
$res = mssql_query("
SELECT TOP 100 ID
FROM Table", $this->suburbDb);
You should initialize both connections with something like this
$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)
{
....
}
Should be something like that.
PS. check the codeigniter guide : CodeIgniter Guide Database Connection
[ EDIT : WITHOUT ACTIVE RECORD ]
after making the connection mssql should be possible to execute by itself like this :
// 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);
you are mixing CI and php native mssql driver.
$this->suburbDb
is CI database object not link identifier:
$this->suburbDb->query('SELECT TOP 100 ID FROM Table');
http://codeigniter.com/user_guide/database/queries.html
OR if you really need then
mssql_query($query, $this->suburbDb->conn_id)
精彩评论