in reply to Not an ARRAY reference Error Help!

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

Replies are listed 'Best First'.
Re^2: Not an ARRAY reference Error Help!
by Anonymous Monk on Jan 13, 2011 at 19:32 UTC
    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