Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am trying to display some records from a table and I am getting an error:"#Not an ARRAY reference at". Could anyone show me how I could still apply this using still a "for loop" to get these records from this table. Here is the code:
... my $dbh = database->connect_mysql(); my $sql = "select * FROM mytable WHERE date < DATE_ADD(NOW(), INTERVA +L -30 DAY)"; my $sth = $dbh->prepare($sql) or "Can't select from table: ",$dbh->err +msg; $sth->execute(); my $data = $sth->fetchrow_hashref(); # <- this line x for (my $i = 0; $i < @{$data}; $i++) { my $user = $data->[$i]{'user'}) || ''; my $id = $data->[$i]{'id'}) || ''; print "Data Test: $user - $id<br>"; } .... #Not an ARRAY reference at line x. Error
Thanks for looking!

Replies are listed 'Best First'.
Re: Not an ARRAY reference Error Help!
by Limbic~Region (Chancellor) on Jan 13, 2011 at 19:02 UTC
    Anonymous Monk,
    I think your issue is actually with:
    for (my $i = 0; $i < @{$data}; $i++) {
    That is, first you fetch a hash reference and then you treat it like an array. On the other hand, you also didn't check the return code of the DB connect and
    my $sth = $dbh->prepare($sql) or "Can't select from table: ",$dbh->err +msg;
    should likely be or die "...". You have more problems than you think. You should start with the smallest working code you can and then add 1 thing at a time verifying it at each stage.

    Cheers - L~R

      Hi, adding this is not a problem: my $sth = $dbh->prepare($sql) or die $dbh->errstr; The issue is with the rest of the code, I just don't understand how I can make this work.
        Anonymous Monk,
        You misunderstand. I pointed out a problem you didn't know you had because you haven't encountered it yet. I also pointed out what your problem was - you are treating a hash reference as an array reference. Instead of just changing things without understanding hoping it will work, try starting with the most basic working code you can (connecting to the DB) and then add things 1 piece at a time. That way, when something breaks you will know it is the last thing you added. You have many more problems with that code then you think you do as it is.

        Cheers - L~R

        my $dbh = database->connect_mysql(); my $sql = "select * FROM mytable WHERE date < DATE_ADD(NOW(), INTERVAL -30 DAY)"; my $records = $dbh->selectall_arrayref( $sql, { Slice => {} } ); foreach my $record ( @$records ) { my $user = $record->{user}; my $id = $record->{id}; print "Data Test: $user - $id<br>"; }
        Check out the 'You may often want to fetch an array of rows where each row is stored as a hash' section of the DBI documentation.

        -derby
Re: Not an ARRAY reference Error Help!
by ww (Archbishop) on Jan 13, 2011 at 19:00 UTC
    Did you consider the possibility that a hashref might not be an array reference?
Re: Not an ARRAY reference Error Help!
by andmott (Acolyte) on Jan 13, 2011 at 20:12 UTC

    Perhaps something like this might work. In your example you try to deference the hash reference as if it were an array reference, hence probably why you were getting that error. Also try using a while loop to continue to get each row until there are no more rows left. This code may or may not work but is probably closer to what you want

    my $dbh = database->connect_mysql(); my $sql = "select * FROM mytable WHERE date < DATE_ADD(NOW(), INTERVA +L -30 DAY)"; my $sth = $dbh->prepare($sql) or "Can't select from table: ",$dbh->err +msg; $sth->execute(); while ( my $hash_ref = $sth->fetchrow_hashref() ) { my $user = $hash_ref->{user} || ''; my $id = $hash_ref->{id} || ''; print "Data Text: $user - $id<br/>"; }