CodeIgniter Forums
database problem? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: database problem? (/showthread.php?tid=48742)



database problem? - El Forum - 01-27-2012

[eluser]Bigil Michael[/eluser]
Hi frnds,

i want to know is it possible to use multiple database for the same project????

can any one answer please??????




database problem? - El Forum - 01-27-2012

[eluser]Aken[/eluser]
Yes. http://ellislab.com/codeigniter/user-guide/database/connecting.html


database problem? - El Forum - 01-27-2012

[eluser]Bigil Michael[/eluser]
now i have created 2 databases like this

Code:
$active_group = "test1";
$active_record = TRUE;

$db['test1']['hostname'] = "localhost";
$db['test1']['username'] = "root";
$db['test1']['password'] = "";
$db['test1']['database'] = "city1";
$db['test1']['dbdriver'] = "mysql";
$db['test1']['dbprefix'] = "";
$db['test1']['pconnect'] = TRUE;
$db['test1']['db_debug'] = TRUE;
$db['test1']['cache_on'] = FALSE;
$db['test1']['cachedir'] = "";
$db['test1']['char_set'] = "utf8";
$db['test1']['dbcollat'] = "utf8_general_ci";


$active_group = "test2";
$active_record = TRUE;

$db['test2']['hostname'] = "localhost";
$db['test2']['username'] = "root";
$db['test2']['password'] = "";
$db['test2']['database'] = "city2";
$db['test2']['dbdriver'] = "mysql";
$db['test2']['dbprefix'] = "";
$db['test2']['pconnect'] = TRUE;
$db['test2']['db_debug'] = TRUE;
$db['test2']['cache_on'] = FALSE;
$db['test2']['cachedir'] = "";
$db['test2']['char_set'] = "utf8";
$db['test2']['dbcollat'] = "utf8_general_ci";

some tables are in the db city1
remaining in city2

but when i execute my project it shows some tables are missing.

can anyone help me???


database problem? - El Forum - 01-27-2012

[eluser]talentsfromindia[/eluser]
You may use as many database as you can for the same project but the point is you need to make a proper connection between the two database which you are using so as to retain all the data without getting the reduct ant one and also without missing the important data by using proper schemas .


database problem? - El Forum - 01-27-2012

[eluser]Bigil Michael[/eluser]
can you point out what is the mistake i made in this code ????




database problem? - El Forum - 01-28-2012

[eluser]InsiteFX[/eluser]
you can only have one active group at a time!

Code:
$DB1 = $this->load->database('test1', TRUE);
$DB2 = $this->load->database('test2', TRUE);

CodeIgniter User Guide - Database Connections



database problem? - El Forum - 01-29-2012

[eluser]Bigil Michael[/eluser]
when i use these steps

Code:
$DB1 = $this->load->database('test1', TRUE);
$DB2 = $this->load->database('test2', TRUE);

it shows an error

Code:
Fatal error: Call to a member function where() on a non-object in C:\xampp\htdocs\citimapia\bangalore\system\application\models\home_model.php

this is the portion it shows an error

Code:
$DB1->where('status', '1');

can anyone help me to solve my problem???


database problem? - El Forum - 01-29-2012

[eluser]Bigil Michael[/eluser]
now i print the value like this
Code:
$DB1 = $this->load->database('test1', TRUE);
$DB2 = $this->load->database('test2', TRUE);
echo "DB1: " . $DB1->conn_id . ", DB2: " . $DB2->conn_id;

and the output is
DB1: Resource id #44, DB2: Resource id #46

but when i use DB2 in my model it shows an error

Quote:Undefined variable: DB2
anybody know what is the mistake i made????...



database problem? - El Forum - 01-30-2012

[eluser]InsiteFX[/eluser]
Try
Code:
$this->db->reconnect();

// or
$this->db->close();
$DB2 = $this->load->database('test2', TRUE);

// also you can clear the db query cache that ci keeps!
$this->db->save_queries = FALSE;    // set it back to TRUE to cache the queries.



database problem? - El Forum - 01-30-2012

[eluser]Bigil Michael[/eluser]
now i solved my problem like this

Code:
class Home_model extends Model {
public $DB1;
public $DB2;
public function Home_model(){
  parent::Model();
  $this->DB1 = $this->load->database('default', TRUE);
        $this->DB2 = $this->load->database('bangalore', TRUE);
}

Code:
$this->DB1->where('status', '1');
$this->DB1->limit(4, 0);
.
.
$this->DB1->query();