关于php:如何解决这个错误“mysql_fetch_assoc()期望参数1是资源,布尔值在”?

How to fix this error “mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in”?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

错误...

Warning: mysql_fetch_assoc() expects
parameter 1 to be resource, boolean
given in
/home/andar/public_html/sistema/admin/cron.php
on line 19

1
2
3
4
5
6
7
8
9
10
11
12
$hoje = strtotime(date("d-m-Y"));

    $db = new DBConfig();
    $db -> config();
    $db->conn();
    $query = mysql_query("SELECT * FROM products WHERE auto_pub =".$hoje) or die(mysql_error());

    while($res = mysql_fetch_assoc($query)) {
        $query = mysql_query("UPDATE products SET publicado = '0' WHERE auto_pub =".$hoje) or die(mysql_error());
    }

    $db->close();


要确保while只在查询成功时执行,您可以编写:

1
2
3
4
if ($query)
while($res = mysql_fetch_assoc($query)) {
    mysql_query("UPDATE products SET publicado = '0' WHERE auto_pub =".$hoje) or die(mysql_error());
}

注意循环中没有$query=赋值。 对于UPDATE,您不想阅读任何结果。 无论如何,你正在用or die检查outcoume。


while之前和while之内使用$query ...将变量的名称更改为$query2,例如:

1
2
3
4
5
6
7
8
9
10
11
12
$hoje = strtotime(date("d-m-Y"));

$db = new DBConfig();
$db -> config();
$db->conn();
$query = mysql_query("SELECT * FROM products WHERE auto_pub =".$hoje) or die(mysql_error());

while($res = mysql_fetch_assoc($query)) {
    $query2 = mysql_query("UPDATE products SET publicado = '0' WHERE auto_pub =".$hoje) or die(mysql_error());
}

$db->close();

希望能帮助到你。


可能是由于语法错误(未引用您的日期),mysql_query返回false