在PDO查询中使用!=排除数据库中的行

I'm trying to exclude certain rows with a PDO query but it is not returning the right value and I'm not seeing my error maybe some of you can help me.

This is the first query that works.

$objGetRecievedChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE recieverid = :recieverid GROUP BY chatid');

Now I want to exclude the chatid's that I get from this query.

foreach ($getRecievedChatFtch as $chatid) {
 echo $chatid['chatid'] . '<BR>';
}

When I echo the above I get the next result:

20920
81586

Wich is correct I want to exclude those two values so I execute the next query:

  $objGetSendChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE ownerid = :ownerid AND chatid != :chatid GROUP BY chatid');

foreach ($getSendChat as $key ) {
 echo $key['chatid'] . '<BR>';
}

But when I echo the above I get the next values

44495
20920
44495

this value 44495 is correct although I only need it once (that is why I GROUP BY chatid) But the value 20920 is one of the values I need to exclude.

Does anyone know what I'm doing wrong?

Thanks in advance!

Whole code:

//Voor de berichten die je hebt ontvangen.
$objGetRecievedChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE recieverid = :recieverid GROUP BY chatid');
$objGetRecievedChat->bindParam('recieverid', $member_id);
$objGetRecievedChat->execute();

$getRecievedChatFtch = $objGetRecievedChat->fetchAll(PDO::FETCH_ASSOC);

//Dit is voor verzonden berichten.
foreach ($getRecievedChatFtch as $chatid) {
  echo $chatid['chatid'] . '<BR>';

  $objGetSendChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE ownerid = :ownerid AND chatid NOT IN(:chatid) GROUP BY chatid');
  $objGetSendChat->bindParam('ownerid', $member_id);
  $objGetSendChat->bindParam('chatid', $chatid['chatid']);

  $objGetSendChat->execute();
  $getSendChat = $objGetSendChat->fetchAll(PDO::FETCH_ASSOC);

  foreach ($getSendChat as $key) {
    echo $key['chatid'] . '<BR>';
  }
}

You're making it wrong: in your foreach loop, you retrieve ALL rows BUT the current one. You must put the query out of the foreach and use a WHERE IN

//Voor de berichten die je hebt ontvangen.
$objGetRecievedChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE recieverid = :recieverid GROUP BY chatid');
$objGetRecievedChat->bindParam('recieverid', $member_id);
$objGetRecievedChat->execute();

$getRecievedChatFtch = $objGetRecievedChat->fetchAll(PDO::FETCH_ASSOC);

//Dit is voor verzonden berichten.
$chatids = array();
foreach ($getRecievedChatFtch as $chatid) {
  echo $chatid['chatid'] . '<BR>';
  $chatids = $chatid['chatid'];
}

$placeholders = implode(',', array_fill('?', count($chatids)));
$objGetSendChat = $objDatabaseMessages->prepare('SELECT * FROM messages WHERE ownerid = ? AND chatid NOT IN(' . $placeholders . ') GROUP BY chatid');

$objGetSendChat->execute(array_merge(array($ownerid, $chatids)));
$getSendChat = $objGetSendChat->fetchAll(PDO::FETCH_ASSOC);

foreach ($getSendChat as $key) {
  echo $key['chatid'] . '<BR>';
}

More or less (because I dislike using WHERE IN with prepared statement. You usually can avoid them with JOIN.


$objGetSendChat = ...

While you use $getSendChat in the foreach.

So I feel we're missing some code here, that contains the error.

Plus, you do a GROUP BY chatid and you get 44495 twice in the result, so the result cannot be the query's one.

Change query to catid NOT IN (xxxx,xxxx).