Database table:
Table Name: friends
user_id | friend_id | accepted | key_id
--------------------------------------------------
123 | 234 | 2 | 1
123 | 456 | 2 | 2
123 | 789 | 1 | 3
222 | 123 | 2 | 4
498 | 123 | 2 | 5
Initial Query (To get confirmed friends)
$user_id=123;
$query="SELECT * FROM friends WHERE
(user_id='$user_id' OR friend_id='$user_id')
AND accepted='2'";
$result=mysqli_query($dbc, $query);
while($row=mysqli_fetch_array($result))
{
if($row['user_id']==$user_id)
{
$friend[]=$row['friend_id'];
}
if($row['friend_id']==$user_id)
{
$friend[]=$row['user_id'];
}
}
print_r($friend);
What I am trying to do is set into the friend array the matching id's where either the user_id is equal to the $user_id or the friend_id is equal to $user_id and the accepted value is 2. At present the above is not returning anything and I am not sure whether I am formatting/creating the array correctly.
What would be the best way to achieve the correct results in this case. I also need to format the array so it can be included in another MYSQL statement using the IN() condition to select from another table.