0

I am using a SELECT query to create an array of values. I am then using another select query, but I wan't to exclude the original array values from being returned. But I keep getting an error with the second query. Am I creating the array correctly? Is my MySQL sytax correct?

My PHP:

// Grabs all the users the logged in user is already friends with or following
$already_following_query= "SELECT recipient FROM relations WHERE sender= '".$user_id."'    
AND status= '1' OR status= '2'";
$already_following_result= mysqli_query($connect, $already_following_query)
    or die('Error with already following query');               
$already_following_array= mysqli_fetch_array($already_following_result);

$suggestions_query= "SELECT * FROM users WHERE user_id NOT IN   
'".$already_following_array."' AND user_id != '".$user_id."'";
$suggestions_result= mysqli_query($connect, $suggestions_query)
or die('Error with suggestions query');
5
  • Would be useful to include the error you get when you say an error is hindering your progress.\ Commented Jan 14, 2014 at 19:06
  • 1
    Also, if $already_following_array is an array, you will need to use implode() Commented Jan 14, 2014 at 19:07
  • 1
    You may want to do a bit of research on how mysqli_fetch_array works, check your SQL syntax (you're missing "()"'s around your IN values), and research how to join an array to a string of commas. There are a few issues here to fix. Commented Jan 14, 2014 at 19:07
  • it will help you ouput $suggestions_query along with the error message. Simply putting $already_following_ARRAY in the query string will not generate the intended query.. Commented Jan 14, 2014 at 19:10
  • You're syntax in your 2nd query is not correct. Commented Jan 14, 2014 at 19:13

1 Answer 1

2

The NOT IN clause requires an array -- you're providing a string.

Try this:

$array_following_array = implode(", ", $array_following_array);

$suggestions_query = "SELECT * FROM users WHERE user_id NOT IN   ('".$already_following_array."') AND user_id != '".$user_id."'";
Sign up to request clarification or add additional context in comments.

Comments

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.