警告:mysql_fetch_assoc()期望参数1是资源,给定字符串

unable to find the error in the loop...please help me

foreach($cat1 as $category){
    $query="SELECT parent FROM categories where id=".$category;
    $result = mysql_query($query);  
    $line = mysql_fetch_assoc($result);
    array_push($cat1,$line['parent']);
    }

Before fetch, you shuld check $result to be sure you works with resource. Also, check if you really have a rows from query with mysql_num_rows.

foreach($cat1 as $category){
    $query="SELECT parent FROM categories where id=".$category;
    $result = mysql_query($query);

    // Check result
    // This shows the actual query sent to MySQL, and the error. Useful for debugging.
    if (!$result) {
        $message  = 'Invalid query: ' . mysql_error() . "
";
        $message .= 'Whole query: ' . $query;
        die($message);
    }

    if(mysql_num_rows($result) > 0) {
        $line = mysql_fetch_assoc($result);
        array_push($cat1,$line['parent']);
    }
    else {
       // do something else
    }
}

The error is due to either an empty result set or an error in your query.

Please use following steps to debug:

  • echo $query first (check if all parameters are there)
  • Write mysql_query($query) or die(mysql_error());

This will show you mysql errors if any.

I would also try debugging the query, by running:

SELECT parent FROM categories where id=3

or insert a valid value ID after id=

Also, is the field being selected: parent a INT or VARCHAR? (my guess is, it's a string, based on your error provided)

if (mysql_fetch_assoc($result) == 1) {

    $score ++;
}

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\answer\get_answer.php on line 24