#!/usr/bin/perl -w use strict; use DBI; ## DB data my $db = "xxx"; my $user = "xxx"; my $pass = "xxx"; my $host = "localhost"; # Connect to DB my $dbh = DBI->connect("DBI:mysql:$db:$host", $user, $pass); my %hash; buildDBHash(0, \%hash); # Build a hash like $hash{part}{of}{the}{hash} = 0 using the Database # Syntax: buildDBHash(parentID, hashref) # Return Value: Hash-Tree sub buildDBHash { # Set variables my $parentID; my $id; my $folders; my $hash; # Load arguments into variables if (defined($_[0])) { $parentID = $_[0]; # Load parentID into variable } if (defined($_[1])) { $hash = $_[1]; # Load parentID into variable } # Set query my $query = "SELECT id,parentID,name FROM dir WHERE parentID=$parentID;"; # Run query my $sth = $dbh->prepare($query); $sth->execute(); # Use the query results ... while (my @result = $sth->fetchrow_array) { my $id = $result[0]; my $parentID = $result[1]; my $name = $result[2]; print $name."\n"; # Add this result to the Hash $hash{$name} = $id; # Create a reference to the hash my $ref = \%hash; # Go one step further $ref = $ref->{$name}; # Call the buildDBHash-function for the child-entries buildDBHash($id, $ref); } }