首页 > 解决方案 > 在 Codeigniter 中将表从一个数据库连接到另一个数据库

问题描述

数据库.php

$db['default'] = array(
    'dsn' => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'main_db',
    'dbdriver' => 'mysqli',
    'dbprefix' => 'sim_',
    'pconnect' => FALSE,
    'db_debug' => FALSE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => FALSE,
);

$db['master'] = array(
    'dsn' => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'secondary_db',
    'dbdriver' => 'mysqli',
    'dbprefix' => 'gap_',
    'pconnect' => FALSE,
    'db_debug' => FALSE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => FALSE,
);

我在 Codeigniter 3 中使用了两个数据库,并且工作正常。现在我面临着在上述数据库中加入表的问题。有没有可能?如果是,那么该怎么做?

示例代码:

public function __construct()
{
    parent::__construct();

    $this->db_ecom = $this->load->database('master', TRUE);
}

public function getOrderDetails() {
   ...       
   $this->db->join('countries', 'countries.country_id=orders.order_country', 'left');
   $q = $this->db_ecom->get('orders');
   ...
}

在上面的代码示例中,$this->db表示main_db所以$this->db_ecom表示secondary_db

main_db- 包含国家表 (country_id, country_name)。
secondary_db- 包含订单表(带有 Country_id 列)。

所以现在我想加入这些表以将国家显示为名称而不是 ID。

标签: phpmysqldatabasecodeigniterjoin

解决方案


作为' Anu '的回答正如马丁提到的这个问题可能是重复的。

$this->db->join('Kalix2.ph_Companies', 'Kalix2.ph_Companies.CompanyName = Asterisk.cdr.CompanyName');

如果我们使用带有PREFIX的表,它将不会被执行

数据库结构:

  • main_db- 表“prefix_countries”
  • secondary_db- 表“prefix_orders”

假设我正在使用dbprefix

$this->db->join('main_db.pre_Companies', 'main_db.pre_Companies.CompanyName = secondary_db.pre_cdr.CompanyName', 'left', FALSE); //You can use NULL replacing 'left'. FALSE in last parameter used to execute the query without enclosing Table Prefix.

解决方案:

public function getOrderDetails() {
   $this->db_ecom->select("countries.country_id, countries.country_name,.."); //Getting values

// Here `$this->db` is 'main_db' and `$this->db_ecom` is 'secondary'.
   $this->db_ecom->join("{$this->db->database}.{$this->db->dbprefix}countries as countries", "countries.country_id={$this->db_ecom->dbprefix}orders.order_country", 'left', FALSE);

   $q = $this->db_ecom->get('orders'); // No need to use $this->db_ecom->from("order"); seperately as @Raymond Nijland said.
   ...
}

最后一个参数中的FALSE用于在不包含表前缀的情况下执行查询。所以我们必须手动输入表前缀,或者我们可以使用$this->db->dbprefix() / $this->db_ecom->dbprefix()方法返回表前缀

我不是在这里硬编码数据库名称。当你上线时它会很有用。所以我使用$this->db->database / $this->db_ecom->database方法来返回Database name

注意:如果您要上线,您应该创建一个授予两个数据库访问权限的用户,否则它会抛出 Mysql 错误。

感谢那些辛勤工作的评论者。
对那些人Stackoverflow来说,不是为了赢得声誉、徽章或特权,它只是为了知道预期问题的答案。

如果您真的面临这种情况,它将很有用。快乐编码。


推荐阅读