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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
pico
  • 247
  • 3
  • 9
  • 20
  • ¿Have yoou tried not using the $active_group clause in the config? – Patroklo Apr 18 '12 at 09:06
  • how to do this correctly is already answered see: http://stackoverflow.com/questions/15348423/codeigniter-multiple-databases-accessing-database-config-in-a-second-database – steve Feb 20 '14 at 19:05

1 Answers1

1

I dont know if this is actually your problem - but why do you return $data from DB1 in the model? Because it never actually reaches DB2 request.

You need to create an array, then combine the results in there.

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;
}

then just change your view output to show each array

edit: "when i run this program i got the error Table ‘vkwow_auth.users’ doesn’t exist" - this doesnt actually sound like a DB connection error - seems more like a query error?

Laurence
  • 58,936
  • 21
  • 171
  • 212