0

I'm working on codeigniter application and application installed on localhost and web server but actually I want that is data should be insert in localhost database and web server database using localhost?

how do i connect web server database in database.php file and how to use web server connection in CI_Controller function?

2 Answers2

0

You can look this post Codeigniter - multiple database connections but basically is like this add this to database.php

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

and you can call it from the model like this

function my_model_method()
{
  $otherdb = $this->load->database('otherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.

  $query = $otherdb->select('first_name, last_name')->get('person');
  var_dump($query);
}
Community
  • 1
  • 1
0

You can do this

function my_model_method()
{
$otherdb = $this->load->database('otherdb', TRUE);
$data= array('name'=>'John');
$database1=$this->db->insert('students', $data); //default db
$database2 = $otherdb->insert('students',$data); //the other db
}
  • data inserted into live database not localhost database? –  Oct 03 '16 at 20:51
  • I don't know what you mean with "Live Database" you can configure the database server where ever you want , could be localhost or from amazon ` $db['db']['hostname'] = "localhost"; or $db['otherdb']['hostname'] = "mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com";` – Elminson De Oleo Baez Oct 03 '16 at 21:26