Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.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()函数在sql中生成和?_Php_Codeigniter_Activerecord - Fatal编程技术网

Php CodeIgniter活动记录或_where()函数在sql中生成和?

Php CodeIgniter活动记录或_where()函数在sql中生成和?,php,codeigniter,activerecord,Php,Codeigniter,Activerecord,我在CodeIgniter中编写了一个活动记录查询,然后我意识到我需要使用OR to WHERE子句。所以我翻阅了文件,找到了我想要的东西。但当我使用它时,它会产生和输出。我找不到关于这个问题的任何其他问题 我正在使用CodeIgniter:2.1.0 以下是我的代码(略为缩减): $this->db->select(“p*),false) 我认为您不需要或\u where。我认为在PHP中需要更好的if/else 您可能想要的逻辑是: if(isset($options['p_id'])) {

我在CodeIgniter中编写了一个活动记录查询,然后我意识到我需要使用OR to WHERE子句。所以我翻阅了文件,找到了我想要的东西。但当我使用它时,它会产生和输出。我找不到关于这个问题的任何其他问题

我正在使用CodeIgniter:2.1.0

以下是我的代码(略为缩减):

$this->db->select(“p*),false)


我认为您不需要
或\u where
。我认为在PHP中需要更好的if/else

您可能想要的逻辑是:

if(isset($options['p_id']))
{
    // if p_id is set, then look for p_id
    $this->db->where('p.p_id=',$options['p_id'],false);
    // if p_id and add_root are set, then look for p_id OR p_id = 1
    if(isset($options['add_root']))
        $this->db->or_where('p.p_id=',1,FALSE);
}
elseif(isset($options['add_root']))
{
    // look for p_id = 1 only
    $this->db->where('p.p_id=',1,FALSE);
}
因为
或_where
是第一个,所以它只是默认为
where
,然后后续的
where
是默认值:一个“and”

你也可以用一系列的
elseif
来写上面的内容,但我认为这不太清楚:

if(isset($options['p_id']) && isset($options['add_root']))
    $this->db
       ->where('p.p_id=',$options['p_id'],false)
       ->or_where('p.p_id=',1,FALSE);
elseif(isset($options['p_id']) || isset($options['add_root']))
    $this->db
       ->where('p.p_id=',
               // if add_root is set, then 1, otherwise p_id
               (isset($options['add_root'])?1:$options['p_id']),false);

您尝试运行的查询顺序中有一个小错误。您可以添加多个where子句,这些子句将转换为介于AND之间的查询。但是如果你想用“或”来代替,你可以用“或”来代替

在您的查询中,您使用了'or_where'子句,这是正确的,但之后使用了'where',这实际上是上一个查询的总和。所以,你必须先使用where子句,然后再使用or_where子句


只要更改顺序,它就会工作。

所以OR需要出现在链接到另一个字段的位置,以便在上执行“OR”,明白了吗。我这样做对我的网站来说是有意义的:
if(isset($options['p\u id')):$this->db->where('p.p\u id=',$options['p\u id',false)//默认情况下获取顶级项目(1)或指定的任何父项目。if(isset($options['add_root'))$this->db->or_where('p.p_id=',1,FALSE);endif因此,在您添加另一个$this->db->的那一刻,您将在sql中得到另一个AND语句。因此,要分组,请使用where\u或在第一个where之后。但是你怎么做括号,你需要吗?例如:从表中选择*,其中(a=b和a=c)和d=e;不能使用条件a=b和a=c。这没有道理。它只能是a=b或a=c。例如,从a=b或a=c的表格中选择*。我也没有发现使用括号有什么不同。它的意思是相同的,带/不带括号。
if(isset($options['p_id']) && isset($options['add_root']))
    $this->db
       ->where('p.p_id=',$options['p_id'],false)
       ->or_where('p.p_id=',1,FALSE);
elseif(isset($options['p_id']) || isset($options['add_root']))
    $this->db
       ->where('p.p_id=',
               // if add_root is set, then 1, otherwise p_id
               (isset($options['add_root'])?1:$options['p_id']),false);