0

I load my first model:

$this->load->model('red_comercial');

then I get data from it:

$Sitios = $this->red_comercial->Obtener_Sitios();

it works fine but when I call the second model:

$config['hostname'] = "localhost";
$config['username'] = "root";
$config['password'] = "mipass";
$config['database'] = "midb";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;

$this->load->model('punto_venta_model','',$config);
$Productos = $this->punto_venta_model->Obtener_Productos();

I get this error:

Table 'Name of my First DataBaseUse.Table of my second database' doesn't exist

the error is that it remains using my first database, I also do this:

$DB1 = $this->load->database('grupo_uno', TRUE);
$DB2 = $this->load->database('grupo_dos', TRUE); 

but it also doesn't work :(

I can do this with mysqli_query but you know...

Lord_Gama
  • 287
  • 2
  • 4
  • 12

1 Answers1

0

I believe this is caused because you can't really use multiple database like that.

Based on my previous answer available here: Codeigniter - best way to use two different database

change your database config like this. for each database create a separate entry:

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "user";
$db['default']['password'] = "database";
$db['default']['database'] = "db1";

$db['db1']['hostname'] = "localhost";
$db['db1']['username'] = "root";
$db['db1']['password'] = "mipass";
$db['db1']['database'] = "midb";

$db['db2']['hostname'] = "localhost";
$db['db2']['username'] = "user";
$db['db2']['password'] = "database";
$db['db2']['database'] = "db2";

//.. and so on

now, in the constructor for each model, do this:

public function __construct(){
    parent::__construct();
    $this->ci = &get_instance();
    $this->db = $this->ci->load->database(DB_FOR_THIS_MODEL, TRUE); 
}

basically, you are setting this this->db variable to the relevant database for that model. you won't need to do this when the model uses the default database.

Community
  • 1
  • 1
Ahmed-Anas
  • 5,471
  • 9
  • 50
  • 72
  • yeah, but I want to connect to multiple databases dynamically,that my site generates, so I can't assign them to the database file, I also changed the $db['default']['pconnect'] value to false. To summary,I have a Global DB on my server then I create a DB dynamically,There is on my Global DB a Table called "Sites" recording the name of my generated dynamically DB, then my default DB is my Global DB and I need to access to my Global DB to get the DB name, and create a new connection to it, with his respective model. I have done this but using mysqli_connect(), losing the benefit of a model. – Lord_Gama Aug 12 '14 at 15:00