This is not caused by any of the code you have posted; however, those echo's are a mess.
Your problem is your query has failed and you have not error checked it. When the query fails it will return false, and then you immediately pass that to a fetch which will trigger an error since it wants a valid resultset resource. Instead, you have given it false or the failure of the query last executed. Use if/else to trap them:
if ($qry = mysql_query($yourquery))
while ($row = mysql_fetch_assoc($qry))
printf('Query has failed: %s' . PHP_EOL, mysql_error());