Search code examples
perlmojolicious

How to fetch an array of arrays in Perl?


I’m trying to fetch some array arrays in Perl but I get a database error, however in the debug I can see the right values.

my $worker_names = $self->glpi->db->query(
        'select user_name from report_users'
    );
 
    my @all = @{$worker_names->arrays->flatten->to_array};
 
    if ($worker_name ne '') {
        @all = ( $worker_name );
    }
 
    # name => [ public, privat, phabricator ]
    my $table;
    for my $user_name (@all) {
        $table->{$user_name}->[0] = '00 Stunden 00 Minuten';
        $table->{$user_name}->[1] = '00 Stunden 00 Minuten';
        $table->{$user_name}->[2] = '00 Stunden 00 Minuten';
    }
 
    $glpi_hours->hashes->each( sub {
        my $row = $_[0];
 
        $table->{$row->{'name'}}->[0] = $row->{'public_actiontime'};
        $table->{$row->{'name'}}->[1] = $row->{'private_actiontime'};
    });
 
    $phabricator_hours->hashes->each( sub {
        my $row = $_[0];
 
        $table->{$row->{'name'}}->[2] = $row->{'phab_time'};
    });

I get the error:

DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at /var/www/perl5/perlbrew/perls/perl-5.28.3/lib/site_perl/5.28.3/Mojo/mysql/Results.pm line 55.       
  # Fetch sql data
52
  my $hash = $to->{type} eq 'hash';
53   
  my $sql_data
54
    = $to->{list} && $hash ? $self->sth->fetchall_arrayref({})
55  
    : $to->{list}          ? $self->sth->fetchall_arrayref
56   
    : $hash                ? [$self->sth->fetchrow_hashref]
57
    :                        [$self->sth->fetchrow_arrayref];
58
59
  # Optionally expand
60
  if ($mode) {

The problem is the array my @all = @{$worker_names->arrays->flatten->to_array}; which isn’t working like it should. I use also mojolicious as a framework.

When i run that in my browser, i can see the values of the fetch, first the error and after that i see the values been in the stash.


Solution

  • The problem was that the result was empty because i gave the values to the array, through the collection, the result stays empty.