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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9205788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:21:07  来源:igfitidea点击:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given

phpmysqlsql

提问by scarhand

i am getting this error when trying to perform the following query:

尝试执行以下查询时出现此错误:

$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");

the problem seems to be the where partcount > 0. it seems to be seeing the 0 as a boolean when trying to make the comparison?

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

回答by GordonM

The value of $sql is probably false. This happens if the query you tried to execute failed to execute, usually due to a syntax error in the SQL.

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

Generally, you want your query code to look like this:

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

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