php CodeIgniter 中的多条件 WHERE 子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15874646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multi-Conditional WHERE Clause In CodeIgniter
提问by cryptic ツ
I want to delete some data like this query that is in core PHP
我想删除一些像这个查询在核心 PHP 中的数据
WHERE user_id=$id AND sender_id=$send_id OR user_id=$send_id AND sender_id=$id
So I tried it in CodeIgniter using Active Record like this :
所以我在 CodeIgniter 中使用 Active Record 进行了尝试,如下所示:
$this->db->where('user_id ', $id);
$this->db->or_where('user_id ', $send_id);
$this->db->where('sender_id ', $send_id);
$this->db->or_where('sender_id ', $id);
But it gives me the wrong result. What am I doing wrong?
但它给了我错误的结果。我究竟做错了什么?
回答by cryptic ツ
$this->db->where(array('user_id' => $id, 'sender_id' => $send_id));
$this->db->or_where(array('user_id' => $send_id, 'sender_id' => $id));
You want to pass an associative array to each one, where it will use AND
between each element in array in query, and using the ->or_where()
will use an OR
in the query. More info is available in the documentation.
您希望将关联数组传递给每个数组,它将AND
在查询中的数组中的每个元素之间使用,并在查询中使用->or_where()
将使用 an OR
。文档中提供了更多信息。
回答by Azam Alvi
Try This
尝试这个
$this->db->where('user_id ', $id);
$this->db->where('sender_id ', $send_id);
$this->db->or_where('sender_id ', $id);
$this->db->where('user_id ', $send_id);