$rank_query = "SET @result_num:=0"; $rank_query2 = "CREATE TEMPORARY TABLE table_name SELECT (@result_num:=@result_num+1) as rank,avg( actual_rating ), being_rated, count(*) FROM ratings GROUP BY being_rated HAVING count(*)>=2 ORDER BY avg(actual_rating) desc"; $rank_query3 = "select * from table_name where being_rated=" .$who_to_rate[0][uid]; //print $rank_query3; $rank_query4 = "select count(*) from ratings group by being_rated having count(*) >=2"; mysql_query($rank_query); mysql_query($rank_query2); $result_x = mysql_query($rank_query4); //print "