如何在codeigniter中使用多个where子句?

时间:2014-02-01 04:30:39

标签: php codeigniter

对于我的数据库查询,我必须在Codeigniter PHP中使用多个where子句查询。我写了这样的代码:

 $this->db->and_where_in('category_name,publication_status','home_headline_sub',1);

但此查询显示浏览器中的数据库查询错误。然后我写了这个查询:

$this->db->where('category_name,publication_status','home_headline_sub',1);

但它仍然会出错。任何人都可以帮我解决这个问题吗?提前谢谢。

3 个答案:

答案 0 :(得分:3)

您可以链接数据库子句,因此您可以将其编写为

$this->db->where('category_name','case')->where('publication_status','case')->where('home_headline_sub','case');

这将生成查询的WHERE子句为

// WHERE category_name = 'case' AND publication_status = 'case' AND home_headline_sub = 'case'

此处的文档:http://ellislab.com/codeigniter/user-guide/database/active_record.html#chaining

答案 1 :(得分:0)

你要在其中使用数组。

$this->db->where(array('category_name'=>case,'publication_status'=>case,'home_headline_sub'=>case));

但我想你想要对三列检查你的价值。你可以用

$this->db->or_where(array('category_name'=>1,'publication_status'=>1,'home_headline_sub'=>1));

我希望它会对你有所帮助。

答案 2 :(得分:0)

//The simple way
$this->db->where('foo_field', 'foo_value')
         ->where('bar_field', 'bar_value')
         ->where('more_field', 'more_value');

//using custom string
//if your sql is really a complex one you can simply write like these

$this->db->where("(foo_filed = 'foo_value') AND (bar_field = 'bar_value') AND (more_field = 'more_value')");

//or may be with something more complex like this
$this->db->where("(foo_filed = 'foo_value') AND ((bar_field = 'bar_value') OR (more_field = 'more_value'))");

//while using a custom string make sure you put them all in the "double quotation marks" and use no ,commas. It is all a single line. The braces are not necessary always but I like to use them.  

Documentation