The reason that this isn't working is that MySQL doesn't currently support SubQueries (having a query in the where clause of another query).
I'm looking at your 2 queries and I'm a bit confused as to the need for both of them. Your first query gets a list of distinct main_group values from the products531 table. And your second query gets a list of distinct charact values from that same table based on the main_group value.
So given this data:
main_group | charact
1 | a
1 | a
1 | b
1 | c
2 | a
2 | d
2 | d
Your looping structure would give you a,b,c for main_group 1 and a,d for main_group 2. Is that what you want or are you looking for just a,b,c,d cause that is what your subquery would return to you. And if you are looking for a,b,c,d then you just need this query:
SELECT DISTINCT charact FROM `products531`;