0

I have a question regarding mysql db and codeigniter framework

I want to use one query accessing multiple DBs using codeigniter which returns one resultset Example as follow:

SELECT A.name, B.class_name
FROM DB1.Table1 AS A, DB2.Table1 AS B
WHERE A.id = B.id

Return one result????

Following are my connection setting in codeigniter

$db['DB2']['hostname'] = 'localhost';
$db['DB2']['username'] = 'connection_name';
$db['DB2']['password'] = 'connection_password';
$db['DB2']['database'] = 'DB2';
$db['DB2']['dbdriver'] = 'mysql';
$db['DB2']['dbprefix'] = '';
$db['DB2']['pconnect'] = TRUE;
$db['DB2']['db_debug'] = TRUE;
$db['DB2']['cache_on'] = FALSE;
$db['DB2']['cachedir'] = '';
$db['DB2']['char_set'] = 'utf8';
$db['DB2']['dbcollat'] = 'utf8_general_ci';
$db['DB2']['swap_pre'] = '';
$db['DB2']['autoinit'] = TRUE;
$db['DB2']['stricton'] = FALSE;


$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'connection_name';
$db['default']['password'] = 'connection_password';
$db['default']['database'] = 'DB1';
$db['default']['dbdriver'] = 'mysql';
$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;

Kindly provide me some sample code which helps to run the above query.

Thanks in advance

4

1 回答 1

1

使用该数据库的 MySQL 用户创建到 DB1 的 1 个连接,例如用户x并在 MySQL 中授予用户x权限DB2,这样您就可以使用一个连接并在查询中访问多个数据库...

于 2012-09-18T09:24:16.413 回答