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 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示