My database has the following setup
productid | productname | category id
and I want to output them like so:
category #1 it
What you want is to order them by the category, not group them.
SELECT * FROM MyTable ORDER BY category_id, product_id
When you iterate through the list, just output a new header whenever the category_id changes.