0

当我需要使用 WHERE 子句同时选择两个列时,我正在尝试删除 MYSQL 数据库中的记录:

 $result3 = mysqli_query($con, " DELETE FROM Waiting WHERE ToUser='$ToEmail' AND ImageID='$ToEmailDB' ");
    if ($result3==false) {
        echo "No images waiting for " . $ToEmail . " for image " . $ToEmailDB;
    }
    else {
        echo "Image and record deleted for " . $ToEmail . " for image " . $ToFileName . ".jpg and record " .                   $GuessImageID;
    }

当我执行此语句$result3 returns true但条目未删除时。我需要在格式中更改什么?字符串回显表中输入的正确数据。

4

4 回答 4

1

是不是应该

"DELETE from Waiting WHERE ToUser = '" . $ToEmail . "' AND ImageID = '" . $ToEmailDB . "'"
于 2013-10-03T05:43:47.387 回答
1
$result3 = mysqli_query($con, "DELETE FROM Waiting WHERE ToUser='".$ToEmail."' AND ImageID='".$ToEmailDB."' ");
于 2013-10-03T05:46:58.527 回答
0

TRUE如果成功执行,您的查询将始终返回。只有在查询失败时才会返回FALSE根据PHP 文档

 mysqli_query() Returns FALSE on failure. For successful SELECT, SHOW, 
 DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object.
 For other successful queries mysqli_query() will return TRUE.

来自 Moby04 的评论

如果您想检查是否确实删除了某些内容,请使用mysqli_affected_rows()功能。

于 2013-10-03T05:20:03.250 回答
0

如果 SQL 查询成功执行,查询将返回 true,即使 DELETE 没有删除数据库中的任何行。

http://php.net/manual/en/mysqli.affected-rows.php

要回答您的问题,您需要找到受影响的行数

$result3 = mysqli_query($con, " DELETE FROM Waiting WHERE ToUser='$ToEmail' AND ImageID='$ToEmailDB' ");

if (!$result3) {
    echo "A database error occurred";
}
else if (mysqli_affected_rows($con) == 0) {
    echo "No images waiting for " . $ToEmail . " for image " . $ToEmailDB;
}
else {
    echo "Image and record deleted for " . $ToEmail . " for image " . $ToFileName . ".jpg and record " . $GuessImageID;
}
于 2013-10-03T05:42:41.167 回答