Recursion queries to the database, apparently, is not very suitable.
I used in addition commentID topicID and parentID else, so for him to sample.
Further, PHP is pereimenovaniya in a tree and the whole tree cache in a serialized form.
And recursion is already at the output depending on the layout. with frameworks suggest, alas, can not.