$mainquery = "SELECT id FROM record_choices"; //grab all possible ids $mainresult = mysql_query($mainquery) or die('Error : ' . mysql_error()); $mainrow = mysql_fetch_row($mainresult); $all_ids = $mainrow['id']; foreach ($all_ids as $value) { //print out the score for each record $query = "SELECT SUM(points) points FROM year_end_votes WHERE record_id = '$value'"; $result = mysql_query($query) or die('Error : ' . mysql_error()); if($result && mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); $all_points = $row['points']; echo "Total score for record $value = {$all_points}
"; } }