dpnv33177 2016-09-18 12:24
浏览 108
已采纳

Codeigniter Query Builder具有IN语句的子句

I am using CodeIgniter query builder. I want to add the having clause in there. My code looks as follows: (i omitted the other parts of the query):

$this->db->having($filterarray);

And i build the filterarray beforehand like this:

$filterarray = array();
        if (!empty($filters['interests'])) {
            $interestids = $this->interests_model->getAllIdsByDescription($filters['interests']);
            $filterarray['interests IN'] = $interestids;
        }

My function getAllIdsByDescription looks like this:

function getAllIdsByDescription($names){
    $res = "(";

    $query = $this->db->where_in('description', $names)
        ->select('interest_id')
        ->get($this->tableName);
    foreach ($query->result() as $interestid) {
        $res .= $interestid->interest_id;
        $res .= ", ";
    }
    $res = substr($res, 0, -2);
    $res .= ")";
    return $res;
}

It translates my query to the following, hence why I have an error:

HAVING interests IN '(7)'

How do i remove the quotes around the (7) ?

  • 写回答

1条回答 默认 最新

  • dsgdg46465 2016-09-18 13:51
    关注

    You can disable the escaping.

    If you are using a database that CodeIgniter escapes queries for, you can prevent escaping content by passing an optional third argument, and setting it to FALSE.

    $this->db->having('user_id',  45);  // Produces: HAVING `user_id` = 45
    $this->db->having('user_id',  45, FALSE);  // Produces: HAVING user_id = 45
    

    Not sure how to implement this when just passing a single array as one argument, but you need the additional FALSE parameter.

    From http://www.codeigniter.com/user_guide/database/query_builder.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗