need some help figuring this out.
My function:
function cleanDatabase()
{
$allowedTime = $this->time - 10;
$query = mysql_query("SELECT * FROM `chatsessions` WHERE `last_active` < ".$allowedTime." AND `public` = 0");
if( mysql_num_rows($query) > 0 ){
while($row = mysql_fetch_array($query)){
//move messages to history for evrey cID
$query2 = mysql_query(" SELECT * FROM `messages`, `chatsessions`
WHERE messages.cID = ".$row['cID']."
AND chatsessions.cID = messages.cID
AND chatsessions.public = 0");
if( mysql_num_rows($query2) > 0 ){
while($row2 = mysql_fetch_array($query2)){
$insert = mysql_query("INSERT INTO `message_history` (`cID`, `mID`, `user_id`, `sender`, `sendtime`, `message`) VALUES
(".$row2['cID'].", ".$row2['mID'].", ".$row2['user_id'].", '".$row2['sender']."', ".$row2['sendtime'].", '".$row2['message']."')");
if($insert)
$delete = mysql_query("DELETE FROM `messages` WHERE `cID` = ".$row2['cID']);
if(!$delete)
return false;
}
}
$query3 = mysql_query("DELETE FROM `chatsessions` WHERE `cID` = ".$row['cID']." AND public = 0");
}
}
}
The function does
select unactive chats the are NOT public 1
Fetch messages from the selected chat
insert messages to history
if inserted delete old messages
delete chat sessions what dont have public = 1
What it needs to do:
select unactive chats the are NOT public 1
Select messages from cID when the last row with that cID is removed (public rows wont get removed so those messages should always stay)
insert messages to history
if inserted delete old messages
delete chat sessions what dont have public = 1
The problem is there are multiple rows in chatsession with the same cID (one for every user and the permanent with pulbic=1 ) i dont know how to NOT delete messages IF there is a chatsession with that ID that has public = 1
edit: step 2 of how it should work