I'm a little bit confused about the way DBI works when I re-use a prepared statement handle inside a while loop. Here's what I mean (table contains a set of tree structure data where elements refer to a parent_id):
my $prepared_handle = $dbh->prepare('SELECT * FROM table WHERE parent_ +id = ?'); : : : get_children(0); sub get_children { my $parent_id = shift; $executed_handle = $prepared_handle->execute($parent_id); while (my $pointer = $executed_handle->fetchrow_hashref) { $results .= $pointer->{id}."\n"; get_children($pointer->{parent_id}); } }
This sort of code doesn't seem to work because the nested call to get_children() appears to be overwriting the previous query. I'm getting:
Top level A Top level B Second Level A Second Level B
and then it stops, instead of continuing like so:
Top level A Top level B Second Level A Second Level B Top level C Top level D
Now this isn't a major problem, I can just re-prepare the $prepared_handle inside get_children() each time instead of using a globally prepared one, but this is less obviously efficient.
Is there an efficient way around this or is this just a limitation of the DBI interface?
(This is on mysql if it makes a difference)
Cheers
MattLG
In reply to Nesting prepared DBI handles by MattLG
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |