There are two ways. MySQL supports a group concatenation operator as an aggregate that allows you to lump multiple record's matching fields into a single returned result using the group_concat operation. The other is as you say. You'll need to fetch both rows out of the recordset, and go from there. You can't do it in a single while loop though, you'll need to capture them likely in an array and then process it after the loop, so it only has value if the entries are all related.
This is why normalization is such a key thing. When you see comma separated lists or serialized arrays in your datastructures, there is likely something incorrect with the storage design itself. Incorrect normalization always leads to complex queries.
header('HTTP/1.1 420 Enhance Your Calm');