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;
    }
}
5
  • Which CodeIgniter version are you using? Commented Jan 14, 2016 at 14:31
  • I recommend looking at stackoverflow.com/a/8269596 and specifically this line $otherdb = $this->load->database('otherdb', TRUE); Commented Jan 14, 2016 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; } Commented Jan 14, 2016 at 14:53
  • I cannot read that, please update your question with the code so that it's easier to read. Thank you Commented Jan 14, 2016 at 15:12
  • Oh sorry, makes sense... just updated the question Commented Jan 14, 2016 at 15:21

1 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Tried your suggestion, however no luck, dont even get a usable error
@NicolaiHolmøThomsen I recommend contacting a professional developer.
@NicolaiHolmøThomsen You can try setting error_reporting(E_ALL); at the top of your page or check out stackoverflow.com/questions/7843406/… 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.
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!
@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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.