user3191005
user3191005

Reputation: 41

Using an array with a MySQL Query

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

Upvotes: 0

Views: 68

Answers (1)

user3141031
user3141031

Reputation:

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."'";

Upvotes: 2

Related Questions