Next time you post code use the PHP tags so your code is color coded and formated nicely.
Well from the code that you are showing it doesn't look like you are getting the results, just the result set id that is returned by tep_db_query(), by the way what is tep_db_query() it's not in the PHP manual. If it's your own function does it return an array that is formatted the way you are expecting it? From the looks of it you are expecting a multi-diminsional array where the first dimension is int indexed and the second dimension is associative.
Now the code I originally posted is expecting that you will get back a dataset that looks something like this:
Category_Name | Item_Name
Cat1 | Item1
Cat1 | Item2
Cat1 | Item3
Cat2 | Item1
Cat2 | Item2
Cat2 | Item3
Cat2 | Item4
Cat3 | Item1
This dataset has all of my items and displays the category name that each item is in. So I will have duplicate category names, but I only print out 1.
There is no way for you to get all items out of your database while only getting 1 category name using a single query.