A good way to get all the nodes for a given tree when you use Adjacency List is to add a column to every row called root_id
or something, so every node knows not only its immediate parent, but also the top node in its tree.
CREATE TABLE Comments (
comment_id INT PRIMARY KEY,
root_id INT,
parent_id INT,
FOREIGN KEY (root_id) REFERENCES Comments (comment_id),
FOREIGN KEY (parent_id) REFERENCES Comments (comment_id)
);
So if you have a hierarchy of 10 -> 20 -> 30, you'd store the following:
INSERT INTO Comments SET comment_id = 10, root_id = 10;
INSERT INTO Comments SET comment_id = 20, root_id = 10, parent_id = 10;
INSERT INTO Comments SET comment_id = 30, root_id = 10, parent_id = 20;
This is similar to how Slashdot stores trees of comments for example.
If you can write a query to fetch all the nodes of a given tree, and each node knows its immediate parent, in the style of the Adjacency List design of storing hierarchical data, you can convert the query result set into a multidimensional array or tree of objects as you fetch it.
See my answer to Convert flat array to the multi-dimentional for code to do this in PHP.