[英]CodeIgniter not returning same results as query
我正在运行查询,从2个不同的表中选择数据:
$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"');
如果在PHPmyAdmin中运行此查询它会返回一些结果,但是当我在CodeIgniter中运行此代码时,它返回一个空数组。
有提示吗? 这真让我抓狂。
我想因为你试图加入两个表并且你没有使用codeigniter $this->db->join()
向下滚动到用户指南,你看到了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();
}
然后vardump它。
大家好,感谢您的回复。
它与使用join子句无关。
我改变了
$this->db->where('b.other_id = "$myId"');
至
$this->db->where('b.other_id', $myId);
并且工作得很好。 不确定原因,因为第一行非常适合简单查询。
尝试这个
$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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.