警告:mysql_fetch_assoc()期望参数1是资源,给定布尔值

时间:2012-02-09 05:18:01

标签: php mysql sql

我在尝试执行以下查询时遇到此错误:

$sql = mysql_query("select c.id as id_car, c.year, c.make, c.model, c.type, c.colour, 
                           (select count(*) from `parts` where `id_car`=c.id and `is_packaged`='yes') as partcount
                           from `cars` as c 
                           where partcount > 0
                           group by c.id
                           order by `id` desc");

问题似乎是where partcount > 0。在尝试进行比较时似乎将0视为布尔值?

1 个答案:

答案 0 :(得分:2)

$ sql的值可能是假的。如果您尝试执行的查询无法执行,通常会由于SQL中的语法错误而发生这种情况。

通常,您希望查询代码如下所示:

if ($result = mysql_query ('SELECT * FROM foo WHERE bar = \'baz\''))
{
    // resultset processing goes here
    while ($row = mysql_fetch_assoc ($result))
    {
    }
}
else
{
    echo (mysql_error ());
}