I recommend the Perl data structures cookbook as a good way to learn about building complex data structures in Perl.
You've had a couple of explanations of the problems in your code. I wanted to mention something a bit deeper. You're creating an array where the indexes are the ids from your database. Unless your ids are small integers, then you're going to create a very sparse array (i.e. one with a lot of missing values that still take up memory). It might make more sense to consider a hash instead of an array.