Here is my mysql table (chatizin)
Id | Sender | Receiver |
---|---|---|
1 | 11 | 6 |
2 | 11 | 96 |
3 | 6 | 96 |
4 | 6 | 11 |
And Here is my php codes by using codeigniter 3
$UserId=11;
$Receiver=6;
$this->db->where(["Sender" => $UserId,"Receiver"=>$Receiver]);
$this->db->or_where(["Sender" => $Receiver,"Receiver"=>$UserId]);
$this->db->order_by("Id DESC");
$sohbetciler = $this->db->get("chatizin")->result();
What i am trying to do is getting below result
Array
(
[0] => stdClass Object
(
[Id] => 4
[Sender] => 6
[Receiver] => 11
)
[2] => stdClass Object
(
[Id] => 1
[Sender] => 11
[Receiver] => 6
)
)
But or_where condition not working as i want. instead it gave me below result. Why it takes Id=3 there is no condition to bring Receiver=96
Array
(
[0] => stdClass Object
(
[Id] => 4
[Sender] => 6
[Receiver] => 11
)
[1] => stdClass Object
(
[Id] => 3
[Sender] => 6
[Receiver] => 96
)
[2] => stdClass Object
(
[Id] => 1
[Sender] => 11
[Receiver] => 6
)
)
$this->db->or_where(["Sender" => $Receiver,"Receiver"=>$UserId]);
in or_where condition "Receiver"=>$UserId this part not working please help me
2
Answers
i found a solution but not sure how accurate
You might have to use query grouping. Here is a tweaked version of the example from the documentation: