2

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;
    }
}
  • Which CodeIgniter version are you using? – MonkeyZeus Jan 14 '16 at 14:31
  • I recommend looking at http://stackoverflow.com/a/8269596 and specifically this line `$otherdb = $this->load->database('otherdb', TRUE);` – MonkeyZeus Jan 14 '16 at 14:38
  • Tried it, but cant get it to work `if (empty($userid) ) { $rs = $this->load->database('social', TRUE); ->select("id, username") ->from("pricecloud_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; }` – Nicolai Holmø Thomsen Jan 14 '16 at 14:53
  • I cannot read that, please update your question with the code so that it's easier to read. Thank you – MonkeyZeus Jan 14 '16 at 15:12
  • Oh sorry, makes sense... just updated the question – Nicolai Holmø Thomsen Jan 14 '16 at 15:21

1 Answers1

0

I think this should work:

public function getUsers($userid = NULL)
{
    $rs = $this->load->database('social', TRUE);
    if (empty($userid) ) {
        $rs->select("id, username")
        ->from("user")
        ->get();
        $userid = $rs->result();
    return $userid;
    }
    elseif (isset($userid)) {
        $rs->select("id, username")
        ->from("user")
        ->where('id', $userid)
        ->get();
        $userid = $rs->result();
    return $userid;
    }
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • Tried your suggestion, however no luck, dont even get a usable error – Nicolai Holmø Thomsen Jan 14 '16 at 15:50
  • @NicolaiHolmøThomsen I recommend contacting a professional developer. – MonkeyZeus Jan 14 '16 at 16:41
  • @NicolaiHolmøThomsen You can try setting `error_reporting(E_ALL);` at the top of your page or check out http://stackoverflow.com/questions/7843406/codeigniter-how-to-catch-db-errors for CodeIgniter specific error handling but I think your initial issue of connecting to a different DB has likely been solved based on the code snippet you provided. – MonkeyZeus Jan 14 '16 at 16:43
  • 1
    i finally succeeded... you suggestion actually did help, however i also did not have the correct user permissions, so after this was changed as well everything worked... Thanks a lot! – Nicolai Holmø Thomsen Jan 16 '16 at 13:07
  • @NicolaiHolmøThomsen That's great to hear! I'm glad you got it sorted out because I do not think I would have guessed that you needed to fix user permissions as well :) – MonkeyZeus Jan 16 '16 at 15:59