1

Hi everyone i am facing an error when i connect multiple database in CI. Can Someone point me to get right way when i connect to multiple DB.

this is my database.php file

$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'cizacl';
$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;

$active_group = 'alternate';
$active_record = TRUE;
$db['alternate']['hostname'] = 'localhost';
$db['alternate']['username'] = 'root';
$db['alternate']['password'] = 'root';
$db['alternate']['database'] = 'vkwow_auth';
$db['alternate']['dbdriver'] = 'mysql';
$db['alternate']['dbprefix'] = '';
$db['alternate']['pconnect'] = TRUE;
$db['alternate']['db_debug'] = TRUE;
$db['alternate']['cache_on'] = FALSE;
$db['alternate']['cachedir'] = '';
$db['alternate']['char_set'] = 'utf8';
$db['alternate']['dbcollat'] = 'utf8_general_ci';
$db['alternate']['swap_pre'] = '';
$db['alternate']['autoinit'] = TRUE;
$db['alternate']['stricton'] = FALSE;  

and then this is retrieve all the records function in my model

function getAll(){
$DB1 = $this->load->database('default',TRUE);
$DB2 = $this->load->database('alternate',TRUE);

//retrieve default DB
$query = $DB1->get('users');

if($query->num_rows() > 0){
foreach($query->result() as $row){
$data[] = $row;    
}
return $data; 
}

//retrieve alternate db
$query = $DB2->get('admins');
if($query->num_rows() > 0){
foreach($query->result() as $row){
$data[] = $row;    
}
return $data; 
}
}

this is my output view code

<p>
<?php foreach($records as $row) : ?>
<h1><?php echo $row->user_username;?></h1>
<?php endforeach; ?>
</p>

<p>
<?php foreach($records as $row) : ?>
<h1><?php echo $row->username;?></h1>
<?php endforeach; ?>
</p>  

when i run this program i got the error Table ‘vkwow_auth.users’ doesn’t exist

I appreciate any feedback/right way to connect multiple DB.

4

1 に答える 1

1

これが実際にあなたの問題であるかどうかはわかりませんが、モデルで DB1 から $data を返すのはなぜですか? 実際に DB2 要求に到達することはないためです。

配列を作成し、そこに結果を結合する必要があります。

function getAll()
{
$DB1 = $this->load->database('default',TRUE);
$DB2 = $this->load->database('alternate',TRUE);

$data = new_array();
//retrieve default DB
$query = $DB1->get('users');

if($query->num_rows() > 0)
{
    foreach($query->result() as $row)
    {
         $data['db1'][] = $row;    
    }
}

//retrieve alternate db
$query = $DB2->get('admins');
if($query->num_rows() > 0)
{
    foreach($query->result() as $row)
    {
        $data['db2'][] = $row;    
    }

}

return $data;
}

次に、ビューの出力を変更して各配列を表示するだけです

編集:「このプログラムを実行すると、テーブル 'vkwow_auth.users' が存在しないというエラーが発生しました」-これは実際には DB 接続エラーのようには聞こえません-クエリ エラーのように思えますか?

于 2012-04-18T10:14:18.947 に答える