What would be a lot faster would be
$query = "SELECT sports_house, COUNT(*) AS count from sudent GROUP BY sports_house";
$result = pg_exec($query);
Fetching arrays from that resultset would give arrays ('sports_house'=>sports_house name, 'count'=>total)