0

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)

Basically I have 3 tables:

memberdetails = mem_id, lastname, firstname .....

classdetails = class_id, classname ...

classmember = class_id, mem_id

I can display a class list using following code

<?php
    $c_query = $conn->query("SELECT * FROM `classdetails` WHERE `class_id`= '$_REQUEST[class_id]'") or die(mysqli_error());
    c_fetch = $c_query->fetch_array();
    $class = $c_fetch['class_id'];
?>   
.......
<?php
  $query = $conn->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->fetch_array()){
?>
<tr>
   <td><?php echo $f_query['firstname']?></td>
   <td><?php echo $f_query['lastname']?></td>
</tr>
<?php
  }
?>

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.

In myPHPAdmin the following works ok

SELECT * FROM memberdetails WHERE mem_id NOT IN (SELECT mem_id FROM classmember WHERE class_id = 3) ORDER BY lastname, firstname ASC 

but I am losing the plot trying to get this to work in PHP

<?php
  $g_query = $conn->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->fetch_array()){
    echo "<option value = ".$g_fetch['mem_id'].">".$g_fetch['firstname'].' '.$g_fetch['lastname']."          </option>";
    }
   ?>

However I can't get this to work in the page. I have tried various ways to format the query.

I daresay this is quite simple, but, at the moment, if I had any hair I would be pulling it out!

OldGuy
  • 9
  • 1
  • 6

2 Answers2

1

You have syntax error on your query.

$g_query = $conn->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());

To this

$g_query = $conn->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());

OR

 $g_query = $conn->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());

Also note that your tried phpMyadmin query and below one is different.

I think what you are looking for is:

$g_query = $conn->query('SELECT * FROM memberdetails WHERE mem_id NOT IN (SELECT mem_id FROM classmember WHERE class_id= '.$class.') ORDER BY lastname, firstname ASC') or die(mysqli_error());
Jigar Shah
  • 6,143
  • 2
  • 28
  • 41
Naushil Jain
  • 434
  • 3
  • 11
-1

There are two mistake I am able to find in your query

  1. use column name instead of *

When you are using sub query and wants to match with a column then you have to write exact column name to which you want to compare otherwise it will give you an error.

  1. you have not properly ended brackets.

Close brackets properly.

As per comment, You have also one more issue.

  1. change $class to .$class

Try Below one.

<?php
  $g_query = $conn->query('SELECT * FROM memberdetails WHERE mem_id NOT IN (SELECT mem_id 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->fetch_array()){
    echo "<option value = ".$g_fetch['mem_id'].">".$g_fetch['firstname'].' '.$g_fetch['lastname']."          </option>";
    }
   ?>
Fahad Anjum
  • 1,246
  • 1
  • 10
  • 19