title: PHP MySQL SELECT WHERE NOT IN
body: <p>Having trouble implementing a bit of code which works in phpMyAdmin, but fails in PHP. (This is my first PHP project, and can't find answers that seem to work)</p>
  
  <p>Basically I have 3 tables:</p>
  
  <p><strong>memberdetails</strong> = mem_id, lastname, firstname .....</p>
  
  <p><strong>classdetails</strong> = class_id, classname ...</p>
  
  <p><strong>classmember</strong> = class_id, mem_id</p>
  
  <p>I can display a class list using following code</p>
  
  <pre><code>&lt;?php
      $c_query = $conn-&gt;query("SELECT * FROM `classdetails` WHERE `class_id`= '$_REQUEST[class_id]'") or die(mysqli_error());
      c_fetch = $c_query-&gt;fetch_array();
      $class = $c_fetch['class_id'];
  ?&gt;   
  .......
  &lt;?php
    $query = $conn-&gt;query("SELECT * FROM `memberdetails` WHERE `mem_id` in (select mem_id from classmember where class_id= '$class')") or die(mysqli_error());
    while($f_query = $query-&gt;fetch_array()){
  ?&gt;
  &lt;tr&gt;
     &lt;td&gt;&lt;?php echo $f_query['firstname']?&gt;&lt;/td&gt;
     &lt;td&gt;&lt;?php echo $f_query['lastname']?&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php
    }
  ?&gt;
  </code></pre>
  
  <p>That all works ok but later on I need to display a list of all the other "members" who are not in the class previously selected.</p>
  
  <p>In myPHPAdmin the following works ok</p>
  
  <pre><code>SELECT * FROM memberdetails WHERE mem_id NOT IN (SELECT mem_id FROM classmember WHERE class_id = 3) ORDER BY lastname, firstname ASC 
  </code></pre>
  
  <p>but I am losing the plot trying to get this to work in PHP</p>
  
  <pre><code>&lt;?php
    $g_query = $conn-&gt;query('SELECT * FROM memberdetails WHERE mem_id NOT IN (SELECT * FROM memberdetails WHERE mem_id in (select mem_id from classmember where class_id= '$class') ORDER BY lastname, firstname ASC') or die(mysqli_error());
    while($g_fetch = $g_query-&gt;fetch_array()){
      echo "&lt;option value = ".$g_fetch['mem_id']."&gt;".$g_fetch['firstname'].' '.$g_fetch['lastname']."          &lt;/option&gt;";
      }
     ?&gt;
  </code></pre>
  
  <p>However I can't get this to work in the page. I have tried various ways to format the query. </p>
  
  <p>I daresay this is quite simple, but, at the moment, if I had any hair I would be pulling it out!</p>