为什么我的PHP脚本(MySQL Query)这么慢?

时间:2011-06-04 23:45:39

标签: php mysql

我运行了几分钟前创建的脚本,但它仍然没有运行完毕。

现在有了,我收到了500内部服务器错误。

编辑---错误发生在第126行,这是while循环的端口:

while ($row == mysql_fetch_assoc($query)) {
            mysql_query("DELETE FROM `Likes` WHERE `accountID` = '$id'");
}

请告诉我是什么原因造成我的剧本太慢了?

if ($param == "closeAccount") {
    $username = NULL;
    $password = NULL;
    $emailAdd = NULL;
    $id = NULL;
    if (isset($_GET['username'])) {
        $username = $_GET['username'];
    }
    else {
        exit("No username is set");
    }
    if (isset($_GET['password'])) {
        $password = md5($_GET['password']);
    }
    else {
        exit("No password is set");
    }
    if (isset($_GET['emailAddress'])) {
        $emailAdd = $_GET['emailAddress'];
    }
    else {
        exit("No email address is set");
    }
    $query = mysql_query("SELECT * FROM `Accounts` WHERE `Username` = '$username' AND `Password` = '$password' AND `Email Address` = '$emailAdd'");
    if (mysql_num_rows($query) < 1) {
        exit("Account doesn't exist");
    }
    while ($row = mysql_fetch_assoc($query)) {
        $id = $row["id"];
    }
    $query = NULL;
    $query = mysql_query("SELECT `id` FROM `Comments` WHERE `accountID` = '$id'");
    while ($row = mysql_fetch_assoc($query)) {
        mysql_query("DELETE FROM `Comments` WHERE `accountID` = '$id'") or die(mysql_error());
    }
    $query = NULL;
    $query = mysql_query("SELECT `id` FROM `Likes` WHERE `accountID` = '$id'");
    while ($row == mysql_fetch_assoc($query)) {
        mysql_query("DELETE FROM `Likes` WHERE `accountID` = '$id'");
    }
    $query = NULL;
    $query = mysql_query("SELECT `id` FROM `Posts` WHERE `accountID` = '$id'");
    while ($row == mysql_fetch_assoc($query)) {
        mysql_query("DELETE FROM `Posts` WHERE `accountID` = '$id'");
    }
    $query = NULL;
    $query = mysql_query("DELETE FROM `Accounts` WHERE `Username` = '$username' AND `Password` = '$password' AND `Email Address` = '$emailAdd'");
    exit("Closed");
}

5 个答案:

答案 0 :(得分:6)

您似乎有很多冗余。我没有你的直接数据库,所以我可能会略微关注你的结构如何,但你可以从跳过下半部分的SELECT语句中受益。

示例:

$query = mysql_query("SELECT * FROM `Accounts` 
    WHERE `Username` = '$username' AND `Password` = '$password' 
    AND `Email Address` = '$emailAdd'");
if (mysql_num_rows($query) < 1) {
    exit("Account doesn't exist or wrong password entered");
}
$row = mysql_fetch_assoc($query);
$id = $row["id"]

$query = NULL;
mysql_query("DELETE FROM `Comments` WHERE `accountID` = '$id'");
mysql_query("DELETE FROM `Likes` WHERE `accountID` = '$id'");
mysql_query("DELETE FROM `Posts` WHERE `accountID` = '$id'");
mysql_query("DELETE FROM `Accounts` WHERE `id` = '$id'");

答案 1 :(得分:2)

500内部服务器错误应导致在服务器上的Web服务器错误日志中记录详细信息。查看您的Web服务器的错误日志,然后从那里开始。

答案 2 :(得分:1)

 $query = mysql_query("SELECT * FROM `Accounts` WHERE `Username` = '$username' AND `Password` = '$password' AND `Email Address` = '$emailAdd'");

可能会失败。

将它们全部改为此

 $query = mysql_query("SELECT * FROM `Accounts` WHERE `Username` = '$username' AND `Password` = '$password' AND `Email Address` = '$emailAdd'") or die(mysql_error());

编辑::哦,是的,这里有一个巨大的SQL注入问题。记得做

$username = mysql_real_escape_string($_GET['usernmame']);

除非您想要注射!

答案 3 :(得分:0)

  • 如果您只想获得一个用户,则无需在此处停留一段时间。只需删除while循环。

    $query = mysql_query("SELECT * FROM `Accounts` WHERE `Username` = '$username' AND `Password` = '$password' AND `Email Address` = '$emailAdd'");
    [...]
    while ($row = mysql_fetch_assoc($query)) {
        $id = $row["id"];
    }
    
  • 如果您将$ query变量设置为NULL,则无效:

    $query = NULL;
    $query = mysql_query("SELECT `id` FROM `Comments` WHERE `accountID` = '$id'");
    
  • 此处未使用SELECT个查询。它们没用,你可以删除它们。

    $query = mysql_query("SELECT `id` FROM `Comments` WHERE `accountID` = '$id'");
    while ($row = mysql_fetch_assoc($query)) {
        mysql_query("DELETE FROM `Comments` WHERE `accountID` = '$id'") or die(mysql_error());
    }
    $query = NULL;
    $query = mysql_query("SELECT `id` FROM `Likes` WHERE `accountID` = '$id'");
    while ($row == mysql_fetch_assoc($query)) {
        mysql_query("DELETE FROM `Likes` WHERE `accountID` = '$id'");
    }
    $query = NULL;
    $query = mysql_query("SELECT `id` FROM `Posts` WHERE `accountID` = '$id'");
    while ($row == mysql_fetch_assoc($query)) {
        mysql_query("DELETE FROM `Posts` WHERE `accountID` = '$id'");
    }
    $query = NULL;
    $query = mysql_query("DELETE FROM `Accounts` WHERE `Username` = '$username' AND `Password` = '$password' AND `Email Address` = '$emailAdd'");
    
  • 在这种情况下,while循环是无用的,因为您始终在执行SAME查询。替换:

    $query = mysql_query("SELECT `id` FROM `Comments` WHERE `accountID` = '$id'");
    while ($row = mysql_fetch_assoc($query)) {
        mysql_query("DELETE FROM `Comments` WHERE `accountID` = '$id'") or die(mysql_error());
    }
    

    由此:

    mysql_query("DELETE FROM `Comments` WHERE `accountID` = '$id'") or die(mysql_error());
    

答案 4 :(得分:0)

如上所述,应该不惜一切代价避免循环中的查询。

按照你指出的那一行,做一下这样的事情:

$ids_to_delete = array();
while ($row = mysql_fetch_assoc($query)) {
       $ids_to_delete[] = (int)  $row['id'];   
}
mysql_query("DELETE FROM `Likes` WHERE `accountID` IN(" . implode(',', $ids_to_delete) . ")");