简体   繁体   中英

get the result of select query with different database in codeigniter

I have declared two databases in my database.php, how i can access the users list of db and db2 instances

        //database initialisation 
        $CI =& get_instance();
        $CI->db1 = $CI->load->database('default', TRUE);
        $CI->db2 = $CI->load->database('stylior_db', TRUE);
         //select query for db2
    $this->$db2->from('users');
    // $this->$CI->db2->where('id',1312);

    $query = $this->$CI->db2->get();
    print_r($query);

    if ( $query->num_rows() > 0 )
    {
        $row = $query->row_array();
        return $row;
    }  

You can use $this keyword directly to get you result. You can use following code to get results:

//database initialisation 
        $CI =& get_instance();
        $this->db1 = $this->load->database('default', TRUE);
        $this->db2 = $this->load->database('stylior_db', TRUE);
         //select query for db2
    $this->$db2->from('users');
    // $this->db2->where('id',1312);

    $query = $this->db2->get();
    print_r($query);

    if ( $query->num_rows() > 0 )
    {
        $row = $query->row_array();
        return $row;
    }  

Try this way

    $db_result = $this->db->get("table_users"); // here default database connected
    $this->db1 = $this->load->database('db1', TRUE);    
    $db1_result = $this->db1->get("table_users")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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