Choose where not equal to multiple values ​​of the keys of the associative array

I made three tables.

Table1 = users . Both userid column names (auto_increment) and username .
TABLE2 = hobby . hobbyid (auto_increment) and hobbyname column names .
Table3 = users_hobbies . column names are FK_userid and FK_hobbyid.

Now hobbyname has cricket, soccer, volleyball .
During registration, the user selects only cricket and football , and I store it in the associative array $ _ SESSION ['hobby_id'] .

But in the profile, he wants to add a few more hobbies.

So, I want to display a list of hobbies that are not equal for $ _SESSION ['hobby_id'].

This, of course, did not work:

foreach($_SESSION['hobby_id'] as $k=>$v)
{

  $query="select hobbyid,hobbyname from hobbies where hobbyid!= $v";
}  
Output was football,volleyball and cricket,volleyball....but i wanted only volleyball

So I want

$query="select hobbyid,hobbyname from hobbies where hobbyid!=(multiple values of session hobby id)";

What code do I need to enter to get the desired result (only volleyball) ?

+5
source share
1 answer

remove the loop foreachand use the NOT INcomparison in MySQL:

$query="
    SELECT hobbyid,hobbyname
    FROM hobbies
    WHERE hobbyid NOT IN (" . implode(',', $_SESSION['hobby_id']) . ")
";
+2
source

All Articles