I have the following function
function availableChatRoom($topic_id){
$max_rooms = 2;
$max_users = 2;
$sqli = "SELECT COUNT(DISTINCT room_num) AS numRooms FROM forum_chat WHERE topic_id = '$topic_id'";
$num_rooms = queryColumnVal($sqli, "numRooms");
$sqlii = "SELECT room_num, COUNT(user_id) AS numUsers FROM forum_chat
WHERE topic_id = '$topic_id' AND (status = 'online' AND status = 'offline') AND chat_msg = ''
GROUP BY room_num
HAVING numUsers < $max_users ORDER BY msg_date";
$num_rooms_with_spaces = mysqlNumRows($sqlii, "room_num");
queryColumnVal($sqlii, "room_num");
if($num_rooms == 0){
return 1;
}
elseif($num_rooms <= $max_rooms){
if($num_rooms_with_spaces == 0){
return $num_rooms + 1;
}
else{
return queryColumnVal($sqlii, "room_num");
}
}
else{
return "none";
}
}
the idea is for the function to return a chat room number with space.
-Every time a user joins the room, a line with no msg is inserted with status online
-Every time a user leaves the room a line with no msg is inserted with status offline
I am now trying to write a function which checks if there are any spare rooms for each topic
The idea is to select the room_num where the number of users who have logged in but not logged out is less than the maximum chatter per room.
Can someone help me with my $sqlii.
Thanks
There are several errors in your query:
(status = 'online' AND status = 'offline')This asserts that your query wil return no now at all. Use:
(status = 'online' OR status = 'offline')or the equivalent(status IN ('online', 'offline'))COUNT(user_id)This does not count the difference of online minus offline users. Correct the logic.
ORDER BY msg_dateIn
GROUP BYqueries, it’s not good to use non-aggregate column, at theHAVING,SELECTorORDER BYclauses, despite that MySQL allows it. Unless that column depends on the grouping expression. Sincemsg_dateobviously does not depend onroom_num, you should replace that with an aggregate function:ORDER BY MAX(msg_date)orORDER BY MIN(msg_date)