Whenever I get stuck with the queries, I copy the query code to a print statement and put it on screen. Often you'll find a key component missing on screen when it looks fine in the code.
e.g.
$query="select x, y, z from table1 left join table2 on ww=zz where x>1 and t!='$var'";
$result=mysql_db_query("DB",$query);
print $query;
print mysql_error();
This will build the query, run it, then show you what it ran and then any errors is hit.