php - Codeigniter,调用多个数据库

标签 php mysql codeigniter

我是 codeigniter 的新手,但正在处理有关多个数据库的问题。数据库位于同一台主机上。

我已经更改了 database.php 文件以包含一个额外的组,除了“默认”新组称为“社会”

$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;

现在在我的代码中调用 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);

我做错了什么,为什么我不能使用 _dbs 从第二个数据库中获取数据?请记住……这里完全是编码新手!这不是我的日常工作:)

更新! 尝试按照 stackoverflow.com/a/8269596 中的描述在此函数中调用数据库,但我仍然无法让它工作。

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;
    }
}

最佳答案

我认为这应该可行:

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;
    }
}

关于php - Codeigniter,调用多个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34791985/

相关文章:

php - 什么是好的PHP分析脚本?

mysql - 客户端成功连接MySQL服务器

php - 使用 php 中的 session 防止对登录的攻击

mysql - 如何选取单行a 100,000,000 x

php - 我正在尝试在 codeigniter 中编写 UPDATE 查询,但由于某种原因,它插入了带有默认值的新记录

PHP:测试两个双变量会在不更改变量的情况下给出不同的结果

php - 使用 clickatell php gateway API 发送多个号码的短信

php - CodeIgniter base_url 导致找不到页面

php - "Call to undefined function array_key_last()"尽管它是在本地运行的 PHP 函数

php - 在放入数据库之前在php中设置日期