$result = mysql_query($query) or die(mysql_error());
As per the billions of other answers on this site, the mysql functions return a boolean FALSE when things fail, which you're not checking for. Even if the query string is syntactically perfect, there's literally hundreds of other reasons why the query might still fail. Blindly assuming success is a very bad thing to do.
You're doing a WHERE somevalue = (subquery). For that kind of comparison, the subquery can return only a single row/value. You'd have to conver to a WHERE somevalue IN (subquery), which allows for multiple rows.