2013-02-03 36 views
4

有人可以帮助我得到完成这个查询用笨的活动记录:让CodeIgniter的数据库查询

我有两个值的int数组:

$prices = array( 
     [0] => 23, 
     [1] => 98 
     ); 
how i can make something like : 

return $this->db->query("select * from product where price IN (?)", array(implode(',',$prices))->result(); 

任何帮助,请。

+0

很抱歉,但我的意思是,我的问题是这样的代码不为我工作,但只是我应该做的,正是用'IN'声明 –

+0

看看我的答案,因为我认为它会做你在问什么。我有一段时间没有使用CI,所以我可能会生锈。 –

回答

1

试试这个(未经测试)

$query = $this->db->from('product') 
        ->where_in('price', implode(',',$prices)) 
        ->get(); 

CodeIgniter Active Record docs都非常好,所以你一定要阅读它。例如,您会注意到select()方法是不必要的,因为我们希望假设所有项目都是*

+0

我相信没有必要'implode'数组'$ prices',否则'implode'后的整个数组将被视为单个字符串。简单地传递数组似乎是语法,如[documentation](http://ellislab.com/codeigniter/user-guide/database/active_record.html)中的示例所示。 –

0

试试这个

return $this->db->query("select * from product where price IN ('". implode(',',$prices)->result()."')");