Welcome Guest, Not a member yet? Register   Sign In
Multiple Database Connections
#11

[eluser]InsiteFX[/eluser]
CodeIgniter Users Guide - Connecting to your Database
#12

[eluser]Unknown[/eluser]
[quote author="WanWizard" date="1282560730"]You don't need to resort to manual coding, just use the database library to connect to multiple databases.

In your config/database.php, create a default entry for your 'app' database. You can connect to that database using
Code:
$this->load->database();

And access it using
Code:
$this->db->...

For your user databases, you could simply add a section to config/database.php, assuming that you have some process in place to create these databases for these users:
Code:
$db['userA']['hostname'] = '...';
// and so on

You can then connect to it using
Code:
// this will make the connection globally available
$CI =& get_instance;
$CI->userdb = $this->load->database('userA', TRUE);
$this->userdb =& $CI->userdb;

If you can't/won't have these userdb configurations in the config file, you can also generate the config array at runtime:
Code:
$db = array();
$db['hostname'] = '...';
// and so on

// connect to the database
$CI =& get_instance;
$CI->userdb = $this->load->database($db, TRUE);
$this->userdb =& $CI->userdb;
[/quote]
Hay WanWizard...
I have try but i get error Using $this when not in object context and...
Code:
// I change $this = $CI... this my config
$CI =& get_instance;
$CI->userdb = $CI->load->database($db, TRUE);
$CI->userdb =& $CI->userdb;
i get error Fatal error: Allowed memory size of...
#13

[eluser]Katie1348[/eluser]
[quote author="jonkenedi" date="1354330680"][quote author="WanWizard" date="1282560730"]You don't need to resort to manual coding, just use the database library to connect to multiple databases.

In your config/database.php, create a default entry for your 'app' database. You can connect to that database using
Code:
$this->load->database();

And access it using
Code:
$this->db->...

For your user databases, you could simply add a section to config/database.php, assuming that you have some process in place to create these databases for these users:
Code:
$db['userA']['hostname'] = '...';
// and so on

You can then connect to it using
Code:
// this will make the connection globally available
$CI =& get_instance;
$CI->userdb = $this->load->database('userA', TRUE);
$this->userdb =& $CI->userdb;

If you can't/won't have these userdb configurations in the config file, you can also generate the config array at runtime:
Code:
$db = array();
$db['hostname'] = '...';
// and so on

// connect to the database
$CI =& get_instance;
$CI->userdb = $this->load->database($db, TRUE);
$this->userdb =& $CI->userdb;
[/quote]
Hay WanWizard...
I have try but i get error Using $this when not in object context and...
Code:
// I change $this = $CI... this my config
$CI =& get_instance;
$CI->userdb = $CI->load->database($db, TRUE);
$CI->userdb =& $CI->userdb;
i get error Fatal error: Allowed memory size of... [/quote]

Incase it helps anyone else coming to this topic, there is a typo in the code.

It should read:
Code:
// I change $this = $CI... this my config
$CI =& get_instance();
$CI->userdb = $CI->load->database($db, TRUE);
$CI->userdb =& $CI->userdb;

The change is that get_instance is a method and needs the ().

K
#14

[eluser]darjap[/eluser]
Hi!

I tried to use this code, since i have to do a similar thing with my application, but i have a problem - when i use the database connection outside the module i have created it, it doesnt work - error: Severity: Notice Message: Undefined variable: CI

I would be really thankful for any help!

Code:
public function connect_company()
    {
     $config = array();
     $config['hostname'] = "localhost";
$config['username'] = "company1";
$config['password'] = "password";
$config['database'] = "company1";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = TRUE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";

$CI =& get_instance();
$CI->company_db = $CI->load->database($config, TRUE);
$CI->company_db =& $CI->company_db;

return TRUE;
    }

Simple usage of the database from another/same module - it works when it is in the same module, does not, when it is in another:

Code:
$query = $this->company_db->get('clients');


foreach ($query->result() as $row)
{
  echo $row->client_name;
}

#15

[eluser]Katie1348[/eluser]
Hi Darjap,

What file and folder are you storing this in? How are you accessing the method/function? Please be specific.

I ask, because if it is a helper, then this will not work as helpers are not part of the class system, but are separate functions.

K
#16

[eluser]darjap[/eluser]
both of them are stored within modules/sth/models/file.php ...it works fine if i store the conection code in config/database.php, but i need a dynamic login into mysql. The function is accessed the regular way:

Code:
$this->load->model('mdl_sessions');
if($this->mdl_sessions->connect_company())
{
   //do stuff...
}


Again, if i use it within the same module(in the "do stuff" section), everything works as it should, but if i redirect to another module and try to do something else with that database, it just gives the error of database not being chosen.
#17

[eluser]Katie1348[/eluser]
[quote author="darjap" date="1376071100"]Hi!

I tried to use this code, since i have to do a similar thing with my application, but i have a problem - when i use the database connection outside the module i have created it, it doesnt work - error: Severity: Notice Message: Undefined variable: CI

I would be really thankful for any help!

Code:
public function connect_company()
    {
     $config = array();
     $config['hostname'] = "localhost";
$config['username'] = "company1";
$config['password'] = "password";
$config['database'] = "company1";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = TRUE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";

$CI =& get_instance();
$CI->company_db = $CI->load->database($config, TRUE);
$CI->company_db =& $CI->company_db;

return TRUE;
    }

Simple usage of the database from another/same module - it works when it is in the same module, does not, when it is in another:

Code:
$query = $this->company_db->get('clients');


foreach ($query->result() as $row)
{
  echo $row->client_name;
}

[/quote]

I think you might have an error in your code.

Code:
$CI->company_db =& $CI->company_db;

I think it should read
Code:
$this->company_db =& $CI->company_db;

That should as far as I can see fix your problem and let you use the company_db method of accessing the database connection etc.

Good luck.

kate




Theme © iAndrew 2016 - Forum software by © MyBB