2

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.

10
  • That code literally as is won't work. Do you have a typo in the fetch call? Commented Apr 2, 2013 at 8:48
  • Thats me being in a rush :) Commented Apr 2, 2013 at 8:49
  • 1
    did your query return any results? Commented Apr 2, 2013 at 8:52
  • 1
    why not re structure your SQL query instead in PHP Commented Apr 2, 2013 at 8:53
  • 1
    you could use union like this: SELECT user_id FROM friends WHERE (user_id='$user_id' OR friend_id='$user_id') AND accepted='2' UNION SELECT friend_id FROM friends WHERE (user_id='$user_id' OR friend_id='$user_id') AND accepted='2' Commented Apr 2, 2013 at 8:55

2 Answers 2

1

Your query should be like this

$query="SELECT *
        FROM friends
        WHERE (user_id = $user_id OR friend_id = $user_id)
        AND accepted = '2'";

Remember you should give integer not string. Its fetching records correctly.

Output

key_id  user_id friend_id   accepted
-------------------------------------
1       123     234         2
2       123     456         2
4       222     123         2
5       489     123         2
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Raheel - I think you have just shown me the error of my ways - in my test database the id values are not integers yet I am treating them as integers. As the query is through an ajax request I am not getting the error message :)
0

You are not passing the needed parameter to mysqli_fetch_array:

while($row=mysqli_fetch_array($result)))

You can read more in the documentation.

2 Comments

Sorry - should have cut and paste from my original code. I have edited the code.
Are you sure that $row['user_id'] and $row['friend_id'] contains something?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.