php - 在 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。
解决方案
作为' 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
来说,不是为了赢得声誉、徽章或特权,它只是为了知道预期问题的答案。
如果您真的面临这种情况,它将很有用。快乐编码。
推荐阅读
- java - 使用 JMeter 测试 Cloudant 连接池
- c++ - 如何避免嵌套模板中的重复模板参数
- sql - 如何在 SQL Server 的列中出现特定值
- javascript - 无法使用带有 load-google-maps-api-2 的 webpack 读取未定义的属性“自动完成”
- common-lisp - 只输出一个空行
- python - 使用 Z3Py 的相同约束集的不同运行时间
- angular - Angular + amazon-cognito-identity-js,为什么我收到错误:未定义全局
- batch-file - 编辑 WindowTitle 以供稍后在 bat 脚本中使用的方法?
- swift - 应用程序崩溃:在对 beginConfiguration 和 commitConfiguration 的调用之间可能不会调用 startRunning?
- amazon-web-services - [错误]:在当前配置文件中找不到 AWS 凭证。- 询问-cli