警告:mysqli_fetch_assoc()期望参数1为mysqli_result,

时间:2015-10-15 15:04:23

标签: php mysql database

我的主页上出现以下错误。 “警告:mysqli_fetch_assoc()要求参数1为mysqli_result,在”

中给出null

我尝试编辑了一些内容,但它一直显示在我的主页顶部。有谁知道这意味着什么以及如何解决这个问题?

$databaseDetect = mysqli_query($link, "SELECT * FROM `detectives` WHERE `status`='volgt'") or die(mysqli_error($link));
    while($itemDetect = mysqli_fetch_assoc($database)) {
        if(strtotime($itemDetect['datum']) < time()) {
            mysqli_query($link, "DELETE FROM `detectives` WHERE `id`='{$itemDetect['id']}'") or die(mysqli_error($link));
        }
    }

谢谢。

1 个答案:

答案 0 :(得分:2)

您的变量名称不匹配 - $ database从未定义过,因此为null,而不是结果。

$database = mysqli_query($link, "SELECT * FROM `detectives` WHERE `status`='volgt'") or die(mysqli_error($link)); // <- this variable was misnamed in the original code, but is fixed here
        while($itemDetect = mysqli_fetch_assoc($database)) { // <- and it is passed to mysqli_fetch_assoc() here, previously you were passing a variable that was null
            if(strtotime($itemDetect['datum']) < time()) {
                mysqli_query($link, "DELETE FROM `detectives` WHERE `id`='{$itemDetect['id']}'") or die(mysqli_error($link));
            }
}

编辑:另见Mark Ba​​ker对OP的评论。除非你有充分的理由循环遍历每个项目并且为了简洁你在这里缩短了代码,否则他的查询结果会相同并且会更快:

DELETE FROM `detectives` WHERE `status` = 'volgt' and `datum` < NOW(); /* or date('Y-m-d H:i:s') if php time is different for some reason */