简体   繁体   English

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

[英]how can i use multiple where clause In codeigniter?

For my database query I have to use multiple where clause query in Codeigniter PHP. 对于我的数据库查询,我必须在Codeigniter PHP中使用多个where子句查询。 I wrote the code like this: 我写了这样的代码:

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

But this query shows database query error in browser. 但是此查询显示浏览器中的数据库查询错误。 Then I wrote this query: 然后我写了这个查询:

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

But it still give error. 但是它仍然给错误。 Can anyone help me to solve this? 谁能帮我解决这个问题? Thanks in advance. 提前致谢。

You can chain database clauses, so you would write it as 您可以链接数据库子句,因此可以将其写为

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

This would generate a query's WHERE clause as 这将生成查询的WHERE子句,如下所示

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

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

you to use array in it. 您要在其中使用数组。

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

but I guess you want to check your value against three columns. 但我想您想对照三栏检查您的价值。 you can use 您可以使用

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

I hope it will help you. 希望对您有帮助。

//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 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM