I am very new to codeigniter,but dealing with an issue regarding multiple db's. The databases are located on the same host.
I have changed the database.php file to include an extra group aside from 'default' new group is called 'social'
$active_group = 'default';
$active_group = 'social';
$active_record = TRUE;
$db['default']['hostname'] = 'xxxx';
$db['default']['username'] = 'xxx';
$db['default']['password'] = 'xxxx';
$db['default']['database'] = 'mysql';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['social']['hostname'] = 'xxx';
$db['social']['username'] = 'xxx';
$db['social']['password'] = 'xxx';
$db['social']['database'] = 'social';
$db['social']['dbdriver'] = 'mysqli';
$db['social']['dbprefix'] = '';
$db['social']['pconnect'] = TRUE;
$db['social']['db_debug'] = FALSE;
$db['social']['cache_on'] = FALSE;
$db['social']['cachedir'] = '';
$db['social']['char_set'] = 'utf8';
$db['social']['dbcollat'] = 'utf8_general_ci';
$db['social']['swap_pre'] = '';
$db['social']['autoinit'] = TRUE;
$db['social']['stricton'] = FALSE;
Now in my code i call the dbs
public function __construct()
{
$dbgroup = !file_exists(".git") ? "default" : "development";
$this->_time = microtime(true);
$this->_db =& DB($dbgroup);
$dbgroup = "social";
$this->_time = microtime(true);
$this->_dbs =& DB($dbgroup);
What am i doing wrong, why cant i use _dbs to fetch data from the second db? Keep in mind... totally coding newbie here! this is not my day job :)
UPDATE! Tried calling the db in this function as described in stackoverflow.com/a/8269596, but i still can't get it to work.
public function getUsers($userid = NULL)
{
if (empty($userid) ) {
$rs = $this->load->database('social', TRUE);
->select("id, username")
->from("user")
->get();
$userid = $rs->result();
return $userid;
}
elseif (isset($userid)) {
$rs = $this->load->database('social', TRUE);
->select("id, username")
->from("user")
->where('id', $userid)
->get();
$userid = $rs->result();
return $userid;
}
}