Single query for retrieving a category path
Assuming that the values in the name column are consistent with the parent_id values (and the resulting tree), you can use a triangular join and GROUP_CONCAT():

SELECT 
    t.category_id,
    GROUP_CONCAT(anc.name ORDER BY anc.name
                 SEPARATOR ' &gt; ') AS path
FROM tablename AS t                         -- for every row in t
  JOIN tablename AS anc                     -- find its ancestors
    ON t.name LIKE CONCAT(anc.name, '%')
GROUP BY
    t.category_id ;


Test in SQLfiddle.