So let's say I have a list of ids, each row has a category assigned to it. So there may be 3 rows assigned to apple, 4 to orange, etc. At best, I can retreive the rows so I fetch all category names... Apple apple apple orange orange orange orange in a while loop. How can I fetch each one only once? So that if orange occurs 4 or even 8billion times, it only outputs once. Basically I want to create a list of categories in a while loop.
Use distinct keyword in your query instead... say the column name of your category is category and say your table name is fruits So your query should be : $sql = "SELECT DISTINCT category FROM fruits"; PHP: Now run a while loop and print every entry.. it will be distinct only
$sql = "SELECT DISTINCT category FROM fruits"; $result = mysql_fetch_array($sql); echo $result['anyfield'];