MattLG has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Nesting prepared DBI handles
by WizardOfUz (Friar) on Nov 08, 2009 at 13:12 UTC | |
|
Re: Nesting prepared DBI handles
by moritz (Cardinal) on Nov 08, 2009 at 13:20 UTC | |
by WizardOfUz (Friar) on Nov 08, 2009 at 13:26 UTC | |
|
Re: Nesting prepared DBI handles
by ruzam (Curate) on Nov 08, 2009 at 16:46 UTC | |
|
Re: Nesting prepared DBI handles
by CountZero (Bishop) on Nov 08, 2009 at 14:34 UTC | |
by MattLG (Beadle) on Nov 08, 2009 at 14:59 UTC | |
|
Re: Nesting prepared DBI handles
by happy.barney (Friar) on Nov 09, 2009 at 11:29 UTC | |
|
Re: Nesting prepared DBI handles
by bot403 (Beadle) on Nov 09, 2009 at 13:40 UTC |