View Full Version : fetchrow_hashref question

12-08-2009, 09:09 PM

Having queried my db and running the loop as shown below, I am confused about why data isn't seemingly accessible in the way it would be stored in a hash.

when I want to output a 'key' and beside it, all its 'values', I get the 'key' outputting each time a value is outputted, like this.

double 2010-01-01 - 2010-03-31
double 2010-04-01 - 2010-09-30

when it should be (I want it to be),

double 2010-01-01 - 2010-03-31
2010-04-01 - 2010-09-30

This is how I get the data from the query and I thought by getting a hashref, it would work as described.

while ( $file_data = $sth->fetchrow_hashref) {
my $product_name = $file_data->{product_name};
my $room_type = $file_data->{room_type};
my $start_date = $file_data->{start_date};
my $end_date=$file_data->{end_date};
my $currency = $file_data->{currency};
my $tariff = $file_data->{tariff};

But I wonder if I should get it in a fetchrow_array and then build a hash for processing. And if this (above), is the correct way to get the data, why do I then need to build the hash?

What am I not making sense of?

Dumper shows me the hashref output so should I make a hash of the hashes like this?


or am I doing something in perl, which mysql/DBI can do better?