newtoperl101:
I think the part that's biting you is the fact that you're trying to use the database recursively. Many (most?) database interfaces let you have only one active statement at a time, so once you go down a level, you're effectively destroying the resultset of the caller. I'd suggest one of two ways to work around it:
- Instead of processing the results as you read them from the database, read the entire set of results into an array. Then loop over the array to further process it.
The disadvantage is that your memory size can increase dramatically, but unless you have wide and deep hierarchies, that's not likely to be an issue.
- Alternatively, you could use multiple database connections--specifically create a new database handle inside child_levels. This way, each statement will use its own database handle, and won't be killed by the routine. The disadvantage is that it consumes multiple database connections.
...roboticus
When your only tool is a hammer, all problems look like your thumb.