You have an error in your SQL syntax near 'ORDER BY p.lft ASC' at line 1
SELECT p.id, p.pgname FROM pg_tree n, pg_tree p WHERE n.lft BETWEEN p.lft AND p.rgt AND n.id = ORDER BY p.lft ASC