Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在codeigniter中使用多个where子句?_Php_Codeigniter - Fatal编程技术网

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

Php 如何在codeigniter中使用多个where子句?,php,codeigniter,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',

对于我的数据库查询,我必须在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);

但它仍然给出了错误。谁能帮我解决这个问题?提前感谢。

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

$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'

此处的文档:

您可以在其中使用数组

$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));
我希望它能帮助你

//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.  

使用代码编写者用户指南一切都有可能重复如果您发现其中一个答案有帮助,请接受并标记为正确。如何使用带有“限制”的where子句。假设我使用限制“2”。如何在模型中编写代码?就像这样$this->db->where('foo','bar')$此->数据库->限制(2);它们必须在您的模型中使用相同的方法(函数)。如果我使用2个category name,两个都调用where子句&如果我想要每个category的2个内容(使用limit 2)…我怎么做?我写了一个代码,但是两个类别都显示了2个内容,但是我想要4个内容。每个类别有2个内容。只要内容来自mysql数据库的同一个表,限制(2)就可以了。类似这样的$this->db->select('foo,bar')->where('foo','f')->where('bar','b')->limit(2)$query=$this->db->get('table');然后你应该得到你想要的4个内容。如果它们来自不同的表,则必须加入它们。公共函数select_all_content_2(){$this->db->select('category_name');$this->db->where('category_name','home_headline_left')->where('category_name','home_headline_right');$this->db->order_by('date_add','DESC this->db->limit 2$query\u result=$this->db->get('content');$home\u headline\u left=$query\u result->result();return$home\u headline\u left;}…..这里是我在模型中编写的代码…但它不起作用..写入此内容后不显示