I just had some help here on Stackoverflow and got the correct query. However he didn't know any PHP so now I need help with that as well.
Here's the query and the PHP:
if($do_resync) {
$sql = "
with MY_VIEW as
(
SELECT
room_user.*,
user.username AS user_name,
user.group_id AS group_id, USER.USER_ID
FROM {$x7->dbprefix}room_users room_user
INNER JOIN {$x7->dbprefix}users user ON
user.id = room_user.user_id
WHERE
room_id IN ({$rooms})
)
SELECT COUNT(id) AS block FROM
blocks AS B, MY_VIEW AS M
WHERE
B.user_id = M.user_id
AND B.blocked_id = B.user_id
";
$st = $db->prepare($sql);
$st->execute(); (THIS IS LINE 230 that the error points at)
$users = $st->fetchAll();
$output['users'] = $users;
}
echo json_encode($output);
And here's the error I get:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MY_VIEW as ( SELECT room_user.*, user.username AS user_name, u' at line 6' in pages/sync.php:230 Stack trace: #0 pages/sync.php(230): PDOStatement->execute() #1 index.php(50): require('/home/vol5_3/by...') #2 {main} thrown in pages/sync.php on line 230
EDIT:
SELECT COUNT(id) AS block FROM
blocks AS B,
(
SELECT
room_user.*,
user.username AS user_name,
user.group_id AS group_id, USER.USER_ID
FROM {$x7->dbprefix}room_users room_user
INNER JOIN {$x7->dbprefix}users user ON
user.id = room_user.user_id
WHERE
room_id IN ({$rooms})
) AS M
WHERE
B.user_id = M.user_id
AND B.blocked_id = B.user_id