Dynamically adding a database to codeigniter

I am working on an application codeigniter

to allow the user to add changes and modify the database.

I want to repeat this code for a loop:

$db['db1']['hostname'] = 'localhost';
$db['db1']['username'] = 'ts4l_wp13';
$db['db1']['password'] = 'O&3D6c(0zD70.^9';
$db['db1']['database'] = 'ts4l_wp13';
$db['db1']['dbdriver'] = 'mysql';
$db['db1']['dbprefix'] = '';
$db['db1']['pconnect'] = FALSE;
$db['db1']['db_debug'] = TRUE;
$db['db1']['cache_on'] = FALSE;
$db['db1']['cachedir'] = '';
$db['db1']['char_set'] = 'utf8';
$db['db1']['dbcollat'] = 'utf8_general_ci';
$db['db1']['swap_pre'] = '';
$db['db1']['autoinit'] = TRUE;
$db['db1']['stricton'] = FALSE;

      

I'm doing it:

@$get_data = mysql_query("SELECT * FROM `database_name`");
    while($getdata =  mysql_fetch_assoc($get_data)){        
    $id='db'.$getdata['id'];
    $iid="$id";
                     $db["$iid"]['hostname'] = 'localhost';
                     $db["$iid"]['username'] = $getdata['username'];
                     $db["$iid"]['password'] = $getdata['password'];
                     $db["$iid"]['database'] = $getdata['name'];
                     $db["$iid"]['dbdriver'] = 'mysql';
                     $db["$iid"]['dbprefix'] = '';
                     $db["$iid"]['pconnect'] = FALSE;
                     $db["$iid"]['db_debug'] = TRUE;
                     $db["$iid"]['cache_on'] = FALSE;
                     $db["$iid"]['cachedir'] = '';
                     $db["$iid"]['char_set'] = 'utf8';
                     $db["$iid"]['dbcollat'] = 'utf8_general_ci';
                     $db["$iid"]['swap_pre'] = '';
                     $db["$iid"]['autoinit'] = TRUE;
                     $db["$iid"]['stricton'] = FALSE;
    }

      

and this block.

// Loading  db and running query.
        $CI = &get_instance();      
        $this->db1 = $CI->load->database('db1', TRUE);
        $this->db1->set($data);
        $this->db1->insert($this->_table_name);
        $id=$this->db1->insert_id();

      

I'm doing it:

$this->load->model('mdb_m');
  $dbms =$this->mdb_m->get();

        if (count($dbms)) {
           foreach ($dbms as $dbm) {
              $mid=$dbm->id;
              $dbc='db'.$mid;
               $CI = &get_instance();   
               $this->db.$mid = $CI->load->database( "$dbc" , TRUE);
               $this->db.$mid->set($data);
               $this->db.$mid->insert($this->_table_name);
           }
        }

      

But it only inserts the first database into the array and then stops and retrieves this "You specified an invalid database connection group" error.

+3


source to share


1 answer


you can get brief information on using multiple databases here. As you mentioned. https://ellislab.com/codeigniter/user-guide/database/connecting.html

for a better approach you can make a loader to load the database. I was in the same problem and got a clear visualization from here



https://coderwall.com/p/_kyjvg/codeigniter-loading-up-multiple-databases

see here and if still confusing please come back to me, thanks.

0


source







All Articles