When using parent/child relationships like this, you have to join the table to itself to get the list of children for any/all parents
table comments
cid autoinc id column
aid int FK of articles table
pid int parent id when child or NULL when parent
uid int FK of users table
comment text
// to get a list of all parents
"SELECT * FROM comments WHERE aid=$aid AND pid IS NULL";
// to extend it to include all children
"SELECT * FROM comments p LEFT JOIN comments c ON p.cid=c.pid
WHERE p.aid=$aid AND p.pid IS NULL
ORDER BY p.cid, c.cid";
That will give you output like this
p.cid | p.aid | p.pid | p.uid | p.comment | c.cid | c.aid | c.pid | c.uid | c.comment
1|21|NULL|18|whatever|NULL
2|21|NULL|19|rhubbarb|18|21|2|57|pie
2|21|NULL|19|rhubbarb| 29|21|2_|38|custard
3|21|NULL|57|waffle|14|21|3|29|pancake
3|21|NULL|57|waffle_|19|21|3|18|scone
3|21|NULL|57|waffle|39|21|3|24|damper
4|21|NULL|33|whatever|8_|21|4|24|you say
5|21|NULL|52|i say___|NULL
The main reason you have to do it this way is because of the sorting. If you just selected all the comments with the same article id then you would have no way to sort the children directly under their respective parents.