简体   繁体   English

从其他数据库代码选择器中选择表

[英]selecting table from other database codeigniter

How can I make this function 我该如何做这个功能

public function get_all_summary($year_dummy){
    $current_year = $this->session->userdata('curr_year');  
    $new_db = $this->load->database('budget_db', TRUE);

    $q = "select * from budget where sy=$current_year
    AND sy_dummy=$year_dummy";
    $query = $new_db->query($q);
    return $query->result();}

to somewhat like this. 有点像这样

public function get_all_summary($year_dummy){
    $current_year = $this->session->userdata('curr_year');  
    $new_db = $this->load->database('budget_db', TRUE);

    $this->db->select('*');
    $this->db->from('budget');
    $this->db->where("sy",$current_year);
    $this->db->where("sy_dummy",$year_dummy);
    $query = $this->db->get();
    return $query->result();}

The top function is correct but the bottom function is obviously wrong(I don't know how to select table from other db). 顶部函数是正确的,但底部函数显然是错误的(我不知道如何从其他数据库中选择表)。 I'm also connecting to other database and I'm selecting table from the other database(budget_db). 我还连接到其他数据库,并从其他数据库(budget_db)中选择表。

Hope you understand my problem. 希望你理解我的问题。

I think you just need to use $new_db which is instance of budget_db. 我认为您只需要使用$new_db ,它是budget_db的实例。

public function get_all_summary($year_dummy){
    $current_year = $this->session->userdata('curr_year');  
    $new_db = $this->load->database('budget_db', TRUE);

    $new_db->select('*');
    $new_db->from('budget');
    $new_db->where("sy",$current_year);
    $new_db->where("sy_dummy",$year_dummy);
    $query = $new_db->get();

    return $query->result();
}

Hope this might be useful for you. 希望这对您有用。

Supposedly, 'budget_db' is the other database you are trying to connect to, make sure it has its own group defined in the database config. 假设“ budget_db”是您要连接的另一个数据库,请确保它在数据库配置中定义了自己的组。 Otherwise, you can connect to it using 否则,您可以使用

public function get_all_summary($year_dummy){
    $current_year = $this->session->userdata('curr_year');
    $config['hostname'] = "hostname";
    $config['username'] = "db_user";
    $config['password'] = "db_pass";
    $config['database'] = "budget_db";
    $config['dbdriver'] = "mysql";
    $new_db = $this->load->database($config, TRUE);

    $new_db->select('*');
    $new_db->from('budget');
    $new_db->where("sy", $current_year);
    $new_db->where("sy_dummy", $year_dummy);
    $query = $new_db->get();

    return $query->result();
}

By adding the parameter TRUE to the load database method, $new_db becomes the database object of budget_db. 通过将参数TRUE添加到装入数据库方法,$ new_db成为budget_db的数据库对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM