mamesaye
mamesaye

Reputation: 2153

perl called with 2 bind variables when 0 are needed

i have this code:

    my (@matches, @vars);  

    if ($date_to) {
            push(@matches, q{ m.mentioned_at <= ? });
            push(@vars, $date_to);
        }

        if ($person) {
            push(@matches, q{ t.name like ? });
            push(@vars, $person);
        }
    if ($show) {
            push(@matches, q{ t.name like ? });
            push(@vars, $show);
        }  
    if (@vars){
        my $where = join (' and ', @matches);
        $where = qq{where $where} if @matches  
        $res = $db->do({ page => $.p, per_page => $.s }, q{
                      select m.id, m.body, m.link,
                      count(distinct(m.id)) as num_items
                      from mentions m
                      $where 
                      group by m.id, m.body, m.link,
                      order by m.created_at desc
               }, @vars );
     }   

     print STDERR Dumper ($where);    
     $VAR1 = 'where  m.mentioned_at <= ?  and  t.name like ? ';`  
     print STDERR Dumper (@vars);   
     $VAR1 = '2012/06/01';  
     $VAR2 = 'Adby Phil';`  

Why do i have this ERROR:

called with 2 bind variables when 0 are needed  
Can't use an undefined value as an ARRAY reference 

It seems like it does not see the $where clause.

Upvotes: 0

Views: 2475

Answers (1)

theglauber
theglauber

Reputation: 29605

I think you got the order of your parameters to do() switched:

$rv  = $dbh->do($statement, \%attr, @bind_values);

Looks like you have \%attr first, then $statement.

Also, be aware that do() doesn't return a statement handle, so you can't fetch the data. You may want a more traditional prepare and execute sequence.

Upvotes: 3

Related Questions