0

当我尝试删除帖子时,我无法使帐户表中的值减少。我想减少的值是“PostCount”、“Likes Count”和“CommentsCount”。目前只有“PostCount”有效。

请你能告诉我我做错了什么:

$arg = mysql_query("SELECT `numberOfLikes` FROM Posts WHERE `id` = '$postID'") or die(mysql_error());

    $query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'");
    while ($row = mysql_fetch_assoc($query)) {
        $b = $row['accountID'];
        mysql_query("UPDATE Accounts SET `numberOfLikes` = (`numberOfLikes` - 1) WHERE `id` = '$b'");
    }
    $arg = mysql_query("SELECT `numberOfComments` FROM Posts WHERE `id` = '$postID'");
    $query = mysql_query("SELECT * FROM Posts WHERE `id` = '$postID'");
    while($row = mysql_fetch_assoc($arg)) {
        $b = $row['accountID'];
        mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` = '$b'");
    }
    $arg = mysql_query("SELECT `PostCount` FROM Accounts WHERE `id` = '$accountID'");
    while ($row = mysql_fetch_assoc($arg)) {
        mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'");
    }
    mysql_query("DELETE FROM Likes WHERE `postID` = '$postID'");
    mysql_query("DELETE FROM Comments WHERE `postID` = '$postID'");
    mysql_query("DELETE FROM Posts WHERE `id` = '$postID' AND `accountID` = '$accountID'") or die(mysql_error());
    exit("Deleted post");
4

2 回答 2

1

首先循环查询会杀死您的执行时间。您希望将其限制为尽可能少的查询,这就是implode发挥作用的地方:

// what is this for? I do not see it being used. $arg = mysql_query("SELECT `numberOfLikes` FROM Posts WHERE `id` = '$postID'") or die(mysql_error());

$query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'");
while ($row = mysql_fetch_assoc($query)) {
    $b[] = $row['accountID'];
}
mysql_query("UPDATE Accounts SET `numberOfLikes` = (`numberOfLikes` - 1) WHERE `id` IN(" . implode(',', $b) . ")") or trigger_error('MySQL Update Failed: ' . mysql_error());

这将更有效率并且具有相同的效果。

下一个查询应该类似:

$arg = mysql_query("SELECT `numberOfComments` FROM Posts WHERE `id` = '$postID'");
// again an extra unnecessary query not being used. $query = mysql_query("SELECT * FROM Posts WHERE `id` = '$postID'");
$b=array();
while($row = mysql_fetch_assoc($arg)) {
    $b[] = $row['accountID'];
}
mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` IN(".implode(',', $b) . ")") or trigger_error(mysql_error());

下一个,我什至不知道你为什么要遍历它:

//$arg = mysql_query("SELECT `PostCount` FROM Accounts WHERE `id` = '$accountID'");
//while ($row = mysql_fetch_assoc($arg)) {
mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'") or trigger_error(mysql_error());
//}

由于您没有在任何地方使用该数据,只需运行更新查询。

实施上述应加快您的应用程序并减少冗余。implode非常方便,几乎总是首选运行 1 个查询与多个查询。我不确定这是否能解决您的问题,但这是朝着修复代码的正确方向迈出的一大步。

于 2011-06-06T00:21:41.393 回答
0

我重新编写了包含我发布的代码的 if 语句,现在它运行良好:

mysql_query("UPDATE Accounts SET `PostCount` = (`PostCount` - 1) WHERE `id` = '$accountID'");
        $query = mysql_query("SELECT * FROM Likes WHERE `postID` = '$postID'") or die(mysql_error());
        while($row = mysql_fetch_assoc($query)) {
            $accID = $row['accountID'];
            mysql_query("UPDATE Accounts SET `Likes Count` = (`Likes Count` - 1) WHERE `id` = '$accID'");
        }
        $query = mysql_query("SELECT * FROM Comments WHERE `postID` = '$postID'");
        while($row = mysql_fetch_assoc($query)) {
            $accID = $row['accountID'];
            mysql_query("UPDATE Accounts SET `CommentsCount` = (`CommentsCount` - 1) WHERE `id` = '$accID'");
        }
        mysql_query("DELETE FROM Likes WHERE `postID` = '$postID'");
        mysql_query("DELETE FROM Comments WHERE `postID` = '$postID'");
        mysql_query("DELETE FROM Posts WHERE `id` = '$postID' AND `accountID` = '$accountID'") or die(mysql_error());
        exit("Deleted post");
于 2011-06-06T00:34:59.783 回答