【问题标题】:codeigniter activerecord where clause issue with own clauses manuallycodeigniter activerecord where子句手动发出自己的子句
【发布时间】:2015-06-18 17:12:27
【问题描述】:

我不确定这是对还是错,但我想在where 子句下面使用。我正在关注Codeigniter手册(Active Record Class章节,$this->db->where();部分,点4)自定义字符串)。

  1. $condition = uid = '4' AND id = '1';
  2. 我的控制器
$fields = "id";
$condition = "";
if($condition) { 
  $condition.=" AND ";
}
if($json_decoded->userId) { 
  if($condition) { 
    $condition.=" AND ";
  }
  $condition.=" uid = '".$json_decoded->userId."'";
}
if($json_decoded->listId) { 
  if($condition) { 
    $condition.=" AND ";
  }
  $condition.=" id = '".$json_decoded->listId."'";
}
 $checkExist = $this->mdl_details->getDetailByIdandUid($fields,$condition);
  1. 我的模型功能
$this->db->select($fields);

$this->db->from(TBL_DETAILS);

if(!empty($condition)) { 
  $this->db->where($condition);
}

$query = $this->db->get()->row();
echo $this->db->last_query(); die;
return $query;

它向我显示如下错误

错误号:1054

“where 子句”中的未知列 'uid = '4' AND id = '1''

SELECT `id` FROM (`guide`) WHERE `uid = '4' AND id = '1'

如何在查询中去掉这个反引号 (`) 字符?

【问题讨论】:

    标签: php codeigniter activerecord


    【解决方案1】:

    将'FALSE'作为$this->db->where()中的第三个参数传递,那么codeigniter不会添加反引号。

    $this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
    

    你的情况

    $this->db->where($condition, NULL, FALSE);
    

    【讨论】:

    • @Syam 能否请您告诉我 MATCH (field) AGAINST ("value") ... 有什么作用?
    • 这意味着您可以编写自己的 where 子句。例如:$this->db->where("uid = '4' AND id = '1'", NULL, FALSE); ,其中 uid 是“字段”,“4”是值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 2011-08-14
    相关资源
    最近更新 更多