Multi-Conditional WHERE Clause In CodeIgniter

2019-04-29 13:00发布

问题:

I want to delete some data like this query that is in core 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 :

$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?

回答1:

$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.



回答2:

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