Reputation: 11
I'm running a query selecting data from 2 different tables:
$this->db->select('a.any_id, a.name');
$this->db->from('table1 a, table2 b');
$this->db->where('a.any_id = b.any_id');
$this->db->where('b.other_id = "$myId"');
If a run this query in PHPmyAdmin it returns some results, but when I run this code in CodeIgniter it returns an empty array.
Any tip? This is driving me crazy.
Upvotes: 1
Views: 294
Reputation: 650
try this
$this->db->select('a.any_id.*,table2.name');
$this->db->from('table1 a');
$this->db->join('tabl2 b', 'a.id=b.id', 'left');
$this->db->where('a.id',$id);
$query = $this->db->get();
return $query->result();
Upvotes: 0
Reputation: 11
Hi all and thanks for the responses.
It had nothing to do with using the join clause.
I just changed
$this->db->where('b.other_id = "$myId"');
to
$this->db->where('b.other_id', $myId);
and worked perfectly. Not sure yet why, as the first line works perfect for simple queries.
Upvotes: 0
Reputation:
I think because you are trying to join two tables and your not using codeigniter $this->db->join()
scroll down on the userguide to you see the join() https://www.codeigniter.com/user_guide/database/query_builder.html
public function example($myID) {
$this->db->select('a.any_id, a.name');
$this->db->from('table1 a', 'LEFT');
// $this->db->from('table1 a');
$this->db->join('table2 b', 'b.any_id = a.any_id', 'LEFT');
$this->db->where('a.any_id', 'b.any_id');
$this->db->where('b.other_id', $myId);
$query = $this->db->get();
return $query->result();
}
Then vardump it.
Upvotes: 0