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条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型