1

I'm trying to join 3 tables from 2 databases in codeigniter. This is my code :

<?php 
    public function get_order_products($orders_id) { 
$this->db->from('database1.tbl_orders'); 
$this->db->join('database1.tbl_order_items','database1.tbl_order_items.orders_id=database1.tbl_orders.orders_id','left'); 
$this->db->join('database2.tbl_customers','database2.tbl_customers.customers_id=database1.tbl_orders.customer_id','left'); 
$this->db->where('database1.tbl_orders.orders_id',$orders_id); 
$result=$this->db->get(); return $result->result(); } 
?>

I have added the second database in my database.php file. Moreover i have added this statement in my model

public function Order_model1()
{
    parent::__construct();
    $this->grossery_db = $this->load->database('grossery_db',TRUE);
}

But still i am not able to get data from my second database. Can someone please tell me why that is happening?

user3783243
  • 5,368
  • 5
  • 22
  • 41
Clara
  • 29
  • 7
  • *"Can someone please tell me how i can join tables from two databases?"* databases as in two separted fysical servers or two databases (`CREATE DATABASE`) in the same server? – Raymond Nijland Jun 14 '19 at 11:00
  • same server..... – Clara Jun 14 '19 at 11:01
  • please refer this [https://stackoverflow.com/questions/14159618/join-query-of-two-databases-in-codeigniter] – M.Hemant Jun 14 '19 at 11:03
  • 1
    MySQL normally supports using `SELECT database1.table1 INNER JOIN database2.table2 ...` syntax codeigniter should supports that also those database prefixs in select(), join() functions and so on . – Raymond Nijland Jun 14 '19 at 11:04
  • i have tried joining like this : db->from('database1.tbl_orders'); $this->db->join('database1.tbl_order_items','database1.tbl_order_items.orders_id=database1.tbl_orders.orders_id','left'); $this->db->join('database2.tbl_customers','database2.tbl_customers.customers_id=database1.tbl_orders.customer_id','left'); $this->db->where('database1.tbl_orders.orders_id',$orders_id); $result=$this->db->get(); return $result->result(); } ?> Still its not working – Clara Jun 14 '19 at 11:17
  • yes it gives an error. "Severity: Notice Message: Undefined property: stdClass::$token" . $ token is the variable that i am trying to get from the table in the second database – Clara Jun 14 '19 at 11:29
  • 1
    I don't see where `token` is being used. Can you expand on the code in the question? Also please include all relevant data in the question (error messages, etc.). Comments are considered temporary and maybe be deleted at anytime. – user3783243 Jun 14 '19 at 11:32

0 Answers0