如何在codeigniter中的get_where中包含多个条件?

时间:2015-03-05 03:07:50

标签: codeigniter

我试图将条件包含在我的get_where上,但它不起作用。单一条件有效,但我需要两个条件。有什么想法吗?

这是我的代码:

$query = $this->db->get_where($this::DB_TABLE, array("$column = " => $id, " $column2 = " => $id2, ));

5 个答案:

答案 0 :(得分:3)

正如我所知,您正在尝试将Active Record与Query结合使用,因此声明条件的最佳方式是:

$this->db->select('*');
$this->db->from('table');
$this->db->where('table.column', $condition1);
$this->db->where('table.column2', $condition2);
$this->db->where('table.column3', $condition3);
$q = $this->db->get();

foreach($q->result() as $row){
var_dump($row);
}

$this->db->select('*');
$this->db->from('table');
$this->db->where(array('table.column' => $condition1, 'table.column2' => $condition2, 'table.column3' => $condition3));
$q = $this->db->get();

foreach($q->result() as $row){
var_dump($row);
}

答案 1 :(得分:1)

$where_array = array(
               $column =>$id,
               $column2=>$id2
              );  
$query = $this->db->get_where($this::DB_TABLE,$where_array);

答案 2 :(得分:0)

相反,我只是这样做了:

$query = "select * from ".$this::DB_TABLE." name where $column=? and $column2 = ?";

$sql = $this->db->query($query, array($id,$id2));

谢谢,Shaiful Islam:)

答案 3 :(得分:0)

我发现一件事与where条件有关。可能对您有帮助。

$ this-> db-> group_start(); $ this-> db-> group_end();

答案 4 :(得分:0)

尝试此代码

$query = $this->db->get_where('table1,table2', array('table1.$id' => $id,'table2.condition' => $condition));
return $query->result_array();