Codeigniter Active Record在多个数据库上选择

时间:2018-01-21 15:19:45

标签: mysql database codeigniter select

我需要一些帮助来弄清楚如何使用Codeigniter中的Active Record从多个数据库中的表中进行选择。

详细说明:我有两个数据库;在第一个中有一个名为users的表,其中包含有关用户的所有详细信息(名字,姓氏等);在第二个数据库中有一个名为credit的表,其中有关于每个用户的信用信息; user_id是链接表的键。

在构造模型中我有:

public function __construct()
    {
        parent::__construct();
        $this->rci_db = $this->load->database('dev', TRUE);
    }

选择数据的查询是:

$this->db->select (" 
            $this->t_users.user_id,
            $this->t_users.username,
            $this->t_users.first_name,
            $this->t_users.last_name,
            $this->t_users.email,
            $this->t_users.lastlogin,
            $this->t_contributers.user_id AS credit
            ");
        $this->db->from($this->t_users);
        $this->db->join($this->rci_db.$this->t_contributers, "$this->t_contributers.user_id = $this->t_users.user_id");

查询无效,我收到以下错误:

Error Number: 1146

Table 'gavsit_data.rci_imgcontributers' doesn't exist

SELECT `users`.`user_id`, `users`.`username`, `users`.`first_name`, `users`.`last_name`, `users`.`email`, `users`.`lastlogin`, `rci_imgcontributers`.`user_id` AS `credit` FROM `users` JOIN `rci_imgcontributers` ON `rci_imgcontributers`.`user_id` = `users`.`user_id` ORDER BY `id` ASC

Filename: models/adm/Users_model.php

非常感谢任何提示...我真的不知道如何解决这个问题。

编辑: 我编辑了拆分选择查询的代码,错误已经消失,但我没有从第二个数据库中获取数据:

   $this->db->select (" 
            $this->t_users.id,
            $this->t_users.username,
            $this->t_users.first_name,
            $this->t_users.last_name,
            $this->t_users.email,
            $this->t_users.last_login,
            $this->t_users.active
            ");
        $this->rci_db->select("
            $this->t_contributers.user_id,
            $this->t_contributers.cbtid
            ");
        $this->db->from($this->t_users);
        $this->rci_db->join("$this->t_contributers", "$this->t_contributers.user_id = $this->db.$this->t_users.id", 'left');

0 个答案:

没有答案
相关问题