my $sth_box_name = $dbh->prepare("SELECT box_name FROM navibox") or die $dbh->errstr; my $sth_links = $dbh->prepare("SELECT id,link_titel,link,categorie,box_name,option,content FROM navigation WHERE box_name=?") or die $dbh->errstr; $sth_box_name->execute or die $dbh->errstr; my @boxes; while( my $box_name = $sth_box_name->fetchrow_array ) { $sth_links->execute($box_name); my @links; while( my $hashref = $sth_links->fetchrow_hashref ) { push @links, { link => $hashref->{link} }; } push @boxes, { box_name => $box_name, box_links => \@links }; } $sth_box_name->finish(); $sth_links->finish();