Could anyone advise as to why this script doesn't work:
#$dbh->do("create table terms (id INTEGER not NULL, # term TEXT,tree TEXT, parent_id INTEGER,left_id # int,right_id int,PRIMARY KEY (id))" # # ); #$dbh->do(qq| INSERT INTO terms VALUES (1,"ANATOMY BODY REGIONS","A01" +,"1","1","8")|); #$dbh->do(qq| INSERT INTO terms VALUES (2,"ABDOMEN","A01.047","1","2", +"")|); #$dbh->do(qq| INSERT INTO terms VALUES (3,"AXILA","A01.133","1","","") +|); #$dbh->do(qq| INSERT INTO terms VALUES (4,"EXTREMITIES","A01.378","1", +"","")|); #$dbh->do(qq| INSERT INTO terms VALUES (5,"ARM","A01.378.209","4",""," +")|); #$dbh->do(qq| INSERT INTO terms VALUES (6,"ELBOW","A01.378.209.235","5 +","","")|); #$dbh->do(qq| INSERT INTO terms VALUES (7,"HAND","A01.378.209.455","5" +,"","")|); #$dbh->do(qq| INSERT INTO terms VALUES (8,"FINGERS","A01.378.209.455.4 +30","7","","")|); my $result= $dbh->prepare("SELECT id FROM terms WHERE parent_id = ?"); my $left = $dbh->prepare("UPDATE terms SET left_id=? WHERE id = ?"); my $right= $dbh->prepare("UPDATE terms SET left_id=? WHERE id = ?"); my $root =1; my $counter = 1; rebuild_tree($root); sub rebuild_tree{ my $idy = shift; $left->execute($ctr++,$idy); $result->execute($idy); while (my $idy = $result->fetchrow_hashref()) { $idy = $i->{id}; rebuild_tree($idy); } $right->execute($ctr++,$idy); }#end of sub
The error message I get is:
DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at s +et_depth1. pl line 67. DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at s +et_depth1. pl line 67.
When I put all the prepare statements in the rebuild_tree sub, the results are not right. Anything wrong with the recursive method or is it a problem with my scoping? Any help would be much appreciated. I am trying to build a nested set database to access some hierarchical data. I am using MySQL as my back-end database.

In reply to Hierarchical Tree Traversal by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.