duanhe1903 2014-10-29 12:19
浏览 210
已采纳

Codeigniter“where”和“or_where”

I'm trying to do the following SQL query with the active record of codeigniter:

SELECT * FROM USERS_OPTIONS WHERE user_id = 3 AND ( option_id = 2 OR option_id = 5 OR option_id = 108 OR .... )

For that, I have this code in codeigniter, but it's not working:

$this -> db -> where("user_id", $user_id);

foreach( $options as $option ){
    $this -> db -> or_where("option_id" , $option["id"]  );
}

$options is an array where I store all the options_id I need.

If I try any of the 2 querys separately, them work. But I'm not able to mix the 2 conditions.

How can I get this?? Thanks in advance.

  • 写回答

5条回答 默认 最新

  • dss89001 2014-10-29 13:19
    关注

    You can use $this->db->where_in() :

    Generates a WHERE field IN ('item', 'item') SQL query joined with AND if appropriate

    $names = array('Frank', 'Todd', 'James');
    $this->db->where_in('username', $names);
    // Produces: AND username IN ('Frank', 'Todd', 'James')
    

    In your case, you will have :

    $this->db->where_in('option_id', $options)
             ->get_where("your_table", array("user_id" => $user_id))->result();
    

    Doc : https://ellislab.com/codeigniter/user-guide/database/active_record.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?