0

I am trying to connect to a new database with Code Igniter, but the query keeps giving the table list of the previous Database.

I want to get all the tables in the new DB, then loop through the new DB's tables to get their field details.

my code is as follows for the new DB connection

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

My function's code looks like this:

$this -> load -> database('adb', TRUE);

$adb = array();

$tables = $this -> db -> list_tables();

foreach ($tables as $table)
{
    $temp = array();

    $fields = $this -> db -> field_data($table);

    $temp['table'] = $table;
    $temp['fields'] = $fields;

    $adb[] = $temp;
}

I get the error stating that a table does not exist in my new database, but the reason for this is the table is in my old DB, but the field_data function is connected to the new DB

I only want the function to get the tables from the new DB.

Thanx in advance


My complete Database config file looks like this

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

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'old_database';
$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;

$db['adb']['hostname'] = 'localhost';
$db['adb']['username'] = 'username';
$db['adb']['password'] = 'password';
$db['adb']['database'] = 'database';
$db['adb']['dbdriver'] = 'mysql';
$db['adb']['dbprefix'] = '';
$db['adb']['pconnect'] = TRUE;
$db['adb']['db_debug'] = TRUE;
$db['adb']['cache_on'] = FALSE;
$db['adb']['cachedir'] = '';
$db['adb']['char_set'] = 'utf8';
$db['adb']['dbcollat'] = 'utf8_general_ci';
$db['adb']['swap_pre'] = '';
$db['adb']['autoinit'] = FALSE;
$db['adb']['stricton'] = FALSE;
CopperRabbit
  • 646
  • 3
  • 7
  • 24

1 Answers1

1
  1. check the name of the new database and the name configured in the database.php of codeigniter
  2. check to see you have not set an active group to the old database i.e `$active_group = 'old_db' i hope that helps
  • I see that there is an Active Group set, should I redefine it when I load the new DB? – CopperRabbit Aug 31 '16 at 12:05
  • when I add `$active_group = 'database'` (which is the new DB, it completely works with the old DB) – CopperRabbit Aug 31 '16 at 12:10
  • I found a solution by writhing my code like in the answer of this [link](http://stackoverflow.com/questions/12767603/codeigniter-best-way-to-use-two-different-database) – CopperRabbit Aug 31 '16 at 12:34