I have a messages table:
mysql> describe messages;
+-----------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user1_id | int(11) | NO | | NULL | |
| user1 | varchar(255) | NO | | NULL | |
| user2_id | int(11) | NO | | NULL | |
| user2 | varchar(255) | NO | | NULL | |
| message | text | YES | | NULL | |
| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user1read | varchar(3) | NO | | NULL | |
| user2read | varchar(3) | NO | | NULL | |
+-----------+--------------+------+-----+-------------------+-----------------------------+
9 rows in set (0.00 sec)
And then I have a blocked table:
mysql> describe blocked;
+-----------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------+------+-----+---------+-------+
| user_id | int(11) | NO | PRI | NULL | |
| blocked_user_id | int(11) | NO | PRI | NULL | |
+-----------------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
What I am trying to do is get all of the data to display in separate divs on a page. This script is called every second. The query I ended up with yielded all messages, no matter the blocked status.
So, let's say my ID is 1, and a blocked user's ID is 5, user 5 should not show up in the list. The same would go for the other user if they logged in.
After messing with that query a while, I attempted a 2nd query. That was even worse because nothing appeared on the page, despite data being returned with manual SQL entries using the same query.
The file is rather small, so I will just post the entire thing:
Edit: The first query is actually displaying all messages, including myself as a message, which is obviously wrong. It appears my queries are more broken than I thought.
<?php
session_start();
include '../../../config/DB.php';
$username = $_SESSION['logged_in']; //to use in queries
try {
$db = new DB(); //new DB object
} catch (Exception $e) {
$e->getMessage();
}
try {
$names = array(); //to store during foreach iterations
//get the id for the other query attempt
$id_result = $db->getRow('SELECT id FROM users WHERE username=?', [$username]);
$id = $id_result['id'];
foreach ($messages_result = $db->getRows('SELECT messages.user1, messages.user2, messages.timestamp, messages.message, messages.user2read, users.avatar
FROM messages
LEFT JOIN users ON messages.user1 = users.username
WHERE messages.user2 = ? AND
(users.id NOT IN (SELECT user_id FROM blocked))
OR (users.id NOT IN (SELECT blocked_user_id FROM blocked))
ORDER BY timestamp DESC', [$username]) as $result) {
$sender = $result['user1'];
$time = $result['timestamp'];
$message = $result['message'];
$avatar = $result['avatar'];
$user2read = $result['user2read'];
//Do this so users will only show once.
//One div per user, and when clicked
//all messages are shown elsewhere on a page.
if (!in_array($sender, $names)) {
$names[] = $sender;
//If the message is unread, show name in bold
//Else show regular text
//$avatar has been removed from the html for now
//It shows up in an <img> tag
if ($user2read === 'no') {
echo '<div id="single_message" data-sender="' . $sender . '"><p style="padding-left:8px;"><p><a class="link" style="font-weight:bold;font-size:16px;" href=' . $sender . '>' . $sender . '</a></strong></p><p style="white-space:pre-wrap;margin-left:8px;margin-right:8px;">' . $message . '</p><p style="padding-left:8px;border-bottom: 1px solid #ccc;">' . $time . '</p></div>';
} else {
echo '<div id="single_message" data-sender="' . $sender . '"><p style="padding-left:8px;"><p><a class="link" href=' . $sender . '>' . $sender . '</a></p><p style="white-space:pre-wrap;margin-left:8px;margin-right:8px;">' . $message . '</p><p style="padding-left:8px;border-bottom: 1px solid #ccc;">' . $time . '</p></div>';
}
} else {
continue;
}
}
} catch (Exception $e) {
}
Here is the 2nd query I tried (I'll just show the foreach). This is the one that shows nothing on the page.
foreach($messages_result = $db->getRows('SELECT * FROM messages m LEFT JOIN blocked b ON ((m.user1_id = b.user_id OR m.user2_id = b.user_id)
AND (m.user1_id = b.blocked_user_id OR m.user2_id = b.blocked_user_id))
WHERE (m.user1_id = 1 OR m.user2_id = 1) HAVING m.user_id IS NULL
ORDER BY timestamp DESC', [$id]) as $result) {
Am I missing something here?