这个PHP SQL不会得到正确的输出

时间:2016-01-12 20:56:59

标签: php sql

这个PHP代码,应该获得用户(内部邮件系统)收到的邮件数量。它工作正常,直到我添加" AND删除=' 0'"在下面的SQL代码中。在SQL表中,所有邮件都设置为" 0"在已删除的输入中。

$user = $get2['username'];

$sql = "SELECT COUNT(*) FROM mailbox WHERE sender = '$user' AND deleted = '0' ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
        echo "". $row['COUNT(*)']. "";
     }
} else {
     echo "0";
}

$conn->close();

4 个答案:

答案 0 :(得分:0)

尝试使用以下代码..

 `$user = $get2['username']`;
    $sql = "SELECT COUNT(*) AS COUNTS FROM mailbox WHERE sender = '$user' AND deleted = '0' ORDER BY id DESC";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
         // output data of each row
         while($row = $result->fetch_assoc()) {
            echo "". $row['COUNTS']. "";
         }
    } else {
         echo "0";
    }

    $conn->close();

答案 1 :(得分:0)

已经解决了。 没有正确连接到数据库,我确信它确实如此。

答案 2 :(得分:0)

  

你写的查询只是一个小错误。   所以请尝试以下查询:

$sql = "SELECT COUNT(*) AS counted FROM mailbox WHERE sender = '$user' AND deleted = '0' ORDER BY id DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
        echo "". $row['counted']. "";
     }

} else {
     echo "Sorry";
}

$conn->close();

我希望这会奏效。

答案 3 :(得分:-1)

AND deleted = '0' ORDER BY更改为AND deleted = 0 ORDER BY

我猜测DELETED列是某种数字数据格式而不是字符串格式。