function deleteCommentsFromTheEndOfTopics() { global $ident, $debug; // Select topics from wich comments will be deleted $result = doquery("SELECT id, maxhsz FROM topicok WHERE maxhsz != 0"); if (mysqli_num_rows($result) > 0) { while(list($topicId, $maxHsz) = mysqli_fetch_row($result)) { getLock("topiclock_$topicId"); // Select comments to be protected doquery("DROP TEMPORARY TABLE IF EXISTS `protected_$topicId`"); doquery("CREATE TEMPORARY TABLE `protected_$topicId` ( `h_uniqid` CHAR(13) NOT NULL) ENGINE=MEMORY"); doquery("INSERT INTO `protected_$topicId` SELECT h_uniqid FROM `comments_$topicId` ORDER BY h_uniqid DESC LIMIT $maxHsz"); // Select comments to be deleted one by one doquery("DROP TEMPORARY TABLE IF EXISTS `onebyone_$topicId`"); doquery("CREATE TEMPORARY TABLE `onebyone_$topicId` ( `h_uniqid` CHAR(13) NOT NULL) ENGINE=MEMORY"); // Search comments which answers or antecedents can be found in another topic doquery("INSERT INTO `onebyone_$topicId` SELECT h_uniqid FROM `comments_$topicId` WHERE ((antecendent_topicid != '$topicId' AND antecendent_topicid != 0) OR (answers NOT LIKE '%-topic_$topicId-%' AND answers != '')) AND h_uniqid NOT IN (SELECT h_uniqid FROM `protected_$topicId`)"); $res = doquery("SELECT h_uniqid FROM `onebyone_$topicId`"); $deleteOneByOneStr = ''; if (mysqli_num_rows($res) > 0) { // There are comments in the table, put the deletion string together $commentArray = array(); while(list($h_uniqid) = mysqli_fetch_row($res)) { $deleteOneByOneStr .= "$topicId-$h_uniqid|"; } } // Lets find comments which are external links containing, and not elements of selected comments before doquery("DROP TEMPORARY TABLE IF EXISTS `commentlink_$topicId`"); doquery("CREATE TEMPORARY TABLE `commentlink_$topicId` ( `h_uniqid` CHAR(13) NOT NULL) ENGINE=MEMORY"); doquery("INSERT INTO `commentlink_$topicId` SELECT h_uniqid FROM `comments_$topicId` WHERE h_uniqid NOT IN (SELECT h_uniqid FROM `onebyone_$topicId`) AND h_uniqid < (SELECT MIN(h_uniqid) FROM `protected_$topicId`) AND (commenttext LIKE '%= (SELECT MIN(h_uniqid) FROM `protected_$topicId`) AND antecendent_uniqid IN (SELECT h_uniqid FROM `answers_$topicId`) AND antecendent_topicid = '$topicId'"); // Delete comments which can be deleted in one query doquery("DELETE FROM `comments_$topicId` WHERE h_uniqid < (SELECT MIN(h_uniqid) FROM `protected_$topicId`) AND h_uniqid NOT IN (SELECT h_uniqid FROM `onebyone_$topicId`)"); doquery("DELETE FROM `lastcomments` WHERE topicid = '$topicId' AND h_uniqid < (SELECT MIN(h_uniqid) FROM `protected_$topicId`) AND h_uniqid NOT IN (SELECT h_uniqid FROM `onebyone_$topicId`)"); doquery("DROP TEMPORARY TABLE IF EXISTS `commentlink_$topicId`"); doquery("DROP TEMPORARY TABLE IF EXISTS `protected_$topicId`"); doquery("DROP TEMPORARY TABLE IF EXISTS `answers_$topicId`"); doquery("DROP TEMPORARY TABLE IF EXISTS `onebyone_$topicId`"); removeLock("topiclock_$topicId"); if ($deleteOneByOneStr != '') { // Delete comments which must be deleted one by one (False is for telling the function not to send mails from the deletion) commenttext_torol($deleteOneByOneStr, False); } // Update bookmarks list($lastCommentUniqId, $lastCommentId) = mysqli_fetch_row(doquery("SELECT h_uniqid, id FROM `comments_$topicId` ORDER BY h_uniqid LIMIT 1")); doquery("UPDATE bookmarks SET commentid = '$lastCommentId', h_uniqid = '$lastCommentUniqId' WHERE uidtid LIKE '%-$topicId' AND h_uniqid < '$lastCommentUniqId'"); } } } Let's see the table structure: First, a table holding comments for one topic (in this table the topicid is 1) CREATE TABLE `lasthsz` ( `id` int(11) unsigned NOT NULL default '0', `hid` int(11) unsigned NOT NULL default '0', `ido` int(11) unsigned NOT NULL default '0', `hostnev` varchar(100) collate latin2_hungarian_ci NOT NULL default '', `topicid` int(11) unsigned NOT NULL default '0', `antecedent` int(11) unsigned NOT NULL default '0', `antecendent_tid` int(11) unsigned NOT NULL default '0', `antecendent_topicid` int(11) unsigned NOT NULL default '0', `antecendent_uniqid` char(13) collate latin2_hungarian_ci NOT NULL default '', `h_uniqid` varchar(13) collate latin2_hungarian_ci NOT NULL default '', `commenttext` text collate latin2_hungarian_ci NOT NULL, `parsedhsz` text collate latin2_hungarian_ci NOT NULL, `edits` varchar(255) collate latin2_hungarian_ci NOT NULL default '', `answers` text collate latin2_hungarian_ci NOT NULL, PRIMARY KEY (`h_uniqid`), KEY `topicid` (`topicid`), KEY `hid` (`hid`), KEY `antecedent` (`antecedent`), KEY `antecendent_uniqid` (`antecendent_uniqid`), FULLTEXT KEY `answers` (`answers`) ) ENGINE=MyISAM DEFAULT CHARSET=latin2 COLLATE=latin2_hungarian_ci; Bookmarks: CREATE TABLE `bookmarks` ( `uidtid` char(12) collate latin2_hungarian_ci NOT NULL default '', `hszid` int(10) unsigned NOT NULL default '0', `h_uniqid` char(13) collate latin2_hungarian_ci NOT NULL, `datum` datetime NOT NULL default '0000-00-00 00:00:00', UNIQUE KEY `uidtid` (`uidtid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin2 COLLATE=latin2_hungarian_ci COMMENT='This table holds bookmarks'; Last comments table CREATE TABLE `lasthsz` ( `id` int(11) unsigned NOT NULL default '0', `hid` int(11) unsigned NOT NULL default '0', `ido` int(11) unsigned NOT NULL default '0', `hostnev` varchar(100) collate latin2_hungarian_ci NOT NULL default '', `topicid` int(11) unsigned NOT NULL default '0', `antecedent` int(11) unsigned NOT NULL default '0', `antecendent_tid` int(11) unsigned NOT NULL default '0', `antecendent_topicid` int(11) unsigned NOT NULL default '0', `antecendent_uniqid` char(13) collate latin2_hungarian_ci NOT NULL default '', `h_uniqid` varchar(13) collate latin2_hungarian_ci NOT NULL default '', `commenttext` text collate latin2_hungarian_ci NOT NULL, `parsedhsz` text collate latin2_hungarian_ci NOT NULL, `edits` varchar(255) collate latin2_hungarian_ci NOT NULL default '', `answers` text collate latin2_hungarian_ci NOT NULL, PRIMARY KEY (`h_uniqid`), KEY `topicid` (`topicid`), KEY `hid` (`hid`), KEY `antecedent` (`antecedent`), KEY `antecendent_uniqid` (`antecendent_uniqid`), FULLTEXT KEY `answers` (`answers`) ) ENGINE=MyISAM DEFAULT CHARSET=latin2 COLLATE=latin2_hungarian_ci; Links: CREATE TABLE `links` ( `lid` int(10) unsigned NOT NULL auto_increment COMMENT 'link id', `mikor` int(10) unsigned NOT NULL COMMENT 'comment time', `h_uniqid` char(13) collate latin2_hungarian_ci NOT NULL COMMENT 'comment uniqid (used at deletion)', `linkurl` char(255) collate latin2_hungarian_ci NOT NULL COMMENT 'link url', PRIMARY KEY (`lid`), KEY `linkurl` (`linkurl`), KEY `h_uniqid` (`h_uniqid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin2 COLLATE=latin2_hungarian_ci COMMENT='This table contains links'; And finally, the topic information table: CREATE TABLE `topicok` ( `nev` varchar(255) collate latin2_hungarian_ci NOT NULL default '', `pure_nev` varchar(100) collate latin2_hungarian_ci NOT NULL default '', `id` int(11) unsigned NOT NULL auto_increment, `velemenyszam` int(11) unsigned NOT NULL default '0', `tulajid` int(11) unsigned NOT NULL default '0', `letiltva` tinyint(3) unsigned NOT NULL default '0', `adminonly` tinyint(3) unsigned NOT NULL default '0', `status` tinyint(3) unsigned NOT NULL default '0', `maxhsz` int(10) unsigned NOT NULL default '0' COMMENT 'delete comments after that amount', `ido` int(11) unsigned NOT NULL default '0', `maxid` int(11) unsigned NOT NULL default '0', `utolsohid` int(11) unsigned NOT NULL default '0', `utolsohszid` int(11) unsigned NOT NULL default '0', `utolsouniqid` char(13) collate latin2_hungarian_ci NOT NULL, `utolsohsz` text collate latin2_hungarian_ci NOT NULL, `leiras` text collate latin2_hungarian_ci NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `utolsouniqid` (`utolsouniqid`), KEY `ido` (`ido`) ) ENGINE=MyISAM DEFAULT CHARSET=latin2 COLLATE=latin2_hungarian_ci;