Welcome Guest, Not a member yet? Register   Sign In
Multiple Database Connections
#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


Messages In This Thread
Multiple Database Connections - by El Forum - 08-23-2010, 03:44 AM
Multiple Database Connections - by El Forum - 08-23-2010, 03:52 AM
Multiple Database Connections - by El Forum - 08-23-2010, 03:58 AM
Multiple Database Connections - by El Forum - 08-23-2010, 04:17 AM
Multiple Database Connections - by El Forum - 09-09-2010, 06:50 PM
Multiple Database Connections - by El Forum - 01-19-2011, 05:27 PM
Multiple Database Connections - by El Forum - 01-19-2011, 06:49 PM
Multiple Database Connections - by El Forum - 01-19-2011, 07:35 PM
Multiple Database Connections - by El Forum - 01-19-2011, 07:51 PM
Multiple Database Connections - by El Forum - 05-01-2012, 04:07 AM
Multiple Database Connections - by El Forum - 05-01-2012, 04:26 AM
Multiple Database Connections - by El Forum - 11-30-2012, 07:58 PM
Multiple Database Connections - by El Forum - 05-12-2013, 03:20 AM
Multiple Database Connections - by El Forum - 08-09-2013, 10:58 AM
Multiple Database Connections - by El Forum - 08-09-2013, 11:23 PM
Multiple Database Connections - by El Forum - 08-10-2013, 12:10 AM
Multiple Database Connections - by El Forum - 08-21-2013, 05:24 AM



Theme © iAndrew 2016 - Forum software by © MyBB