$result = mysql_query("SELECT catID FROM blah ORDER BY catID DESC"); $row = mysql_fetch_array($result); /* Process all the items in each category */ for ($i=1; $i<=$row['catID']; $i++) { $result = mysql_query("SELECT * from blah WHERE catID=".$i.""); $row = mysql_fetch_array($result); /* Work with row here */ }