This error almost always means that your query didn't work the way you wanted it to, and as a result $query is null (or false?) and not a query result object. Try echoing db->errorinfo()
and see if it tells you what went wrong.
You can also try pasting the text of your query into mysql directly and seeing what the result is. If that appears to work correctly, then the problem might be with your database connection code.