0

I am using codeigniter framework. I am not much experienced in this framework. I need help of you guys. I want to join two table present in two different database. In both the table I have one common column. I understand, first I have to create a separate database group inside the file database.php. I have created the group and am able to use ths group in my model separately. I have also loaded another group in my model "default group". Whenever, I am trying to use each group separately it's working without any issue. But I am struggling how to make join on two database using these two database group.

Now I want to join two tables of different database using these two separate group. But I am not sure where exactly I am doing the mistake.

This is my model file.

class Bar_graph extends CI_Model {

                public function __construct () {
                     parent::__construct();
                     $this->db=   $this->load->database('default', TRUE);//This is the default group
                     $this->db2 = $this->load->database('db2', TRUE); //This is the new group I have created
                 }
 //kalix2 and Asterik are my two different database               
               public function join_two_database ()
                {    
                      $cust_id=2;
                      $this->db->select('Kalix2.ph_Companies.CompanyName');
                      $this->db2->select_sum('Asterik.cdr.call_length_billable');
                      $this->db2->select('Asterik.cdr.calldate');
                      $this->db->where('Kalix2.ph_Companies.Cust_ID',$cust_id);
                      $this->db->from('Kalix2.ph_Companies');
                      $this->db2->group_by('Asterik.cdr.CompanyName');
                      $this->db->limit(5);
                      $this->db->join('Asterik.cdr','Kalix2.ph_Companies.CompanyName = Asterik.cdr.CompanyName','inner');
                      $query = $this->db->get();
                     if ($query->num_rows > 0) {
                     return $query-> result(); 
                    }
                }

2 Answers 2

1

AFAIK, you can't join these in one statement. You only have one connection to a database at a time. You'd have to run a query on one database, run a query on the second database and then manipulate the results of both within PHP.

Sign up to request clarification or add additional context in comments.

Comments

0

function join_two_table() {

    $this->db->select("database1.tablename.first_name,database1.tablename.last_name,tablename.*");

    $this->db->from('database2.tablename');

    $this->db->join('database1.tablename', 'database2.tablename.user_id = database1.tablename.user_id');

    $this->db->where('database2.tablename.status', 'open');

    $this->db->order_by('database2.tablename.id', 'DESC');

    $query = $this->db->get();
    return $query->result();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.