As you did not accept the answer, but I thought that I would send my method of processing trees in mysql and php. (one db call for non-recursive sproc)
The full script is here: http://pastie.org/1252426 or see below ...
Hope this helps :)
Php
<?php $conn = new mysqli("localhost", "foo_dbo", "pass", "foo_db", 3306); $result = $conn->query(sprintf("call product_hier(%d)", 3)); echo "<table border='1'> <tr><th>prod_id</th><th>prod_name</th><th>parent_prod_id</th> <th>parent_prod_name</th><th>depth</th></tr>"; while($row = $result->fetch_assoc()){ echo sprintf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", $row["prod_id"],$row["prod_name"],$row["parent_prod_id"], $row["parent_prod_name"],$row["depth"]); } echo "</table>"; $result->close(); $conn->close(); ?>
SQL
drop table if exists product; create table product ( prod_id smallint unsigned not null auto_increment primary key, name varchar(255) not null, parent_id smallint unsigned null, key (parent_id) )engine = innodb; insert into product (name, parent_id) values ('Products',null), ('Systems & Bundles',1), ('Components',1), ('Processors',3), ('Motherboards',3), ('AMD',5), ('Intel',5), ('Intel LGA1366',7); delimiter ; drop procedure if exists product_hier; delimiter
source share