DBD :: Pg :: st failed: ERROR: Column "***" does not exist

This part of my code. I am doing a SELECT using the post id obtained from gmail. Msg_id is stored in the database as base64 and without "<>" symbols.

my $inbox = $imap->select("Inbox") or die "Select error: ", $imap->LastError, "\n";
my @mails = ( $imap->unseen );
foreach my $msgid (@mails) {
    my $m_id = $imap->parse_headers( $msgid, "Message-id" )->{"Message-id"}->[0] . "\n";
    $m_id =~ s/[<\>]//g;
    $m_id = encode_base64($m_id);
    $m_id =~ s/\r?$//;
    my $q1 = "select id from mails.mails_in where user_id=$param[5] and message_id=$m_id and user_remote_id=$param[6]";
    $sth = $dbh->prepare($q1);
    $rv  = $sth->execute();
    my @array;

    while ( @array = $sth->fetchrow_array() ) {
        foreach my $i (@array) {
            print "$i\t";
        }
        print "\n";
    }
}

      

But getting this error.

DBD::Pg::st execute failed: ERROR:  column "zdjkot..." does not exist
LINE 1: ...mails.mails_in where user_id=15206 and message_id=ZDJkOTQ1NT...
                                                             ^ at ./script.pl line 65.

      

I tried to use the existing msg_id, from the database - the result is similar. Another SELECT works correctly. A similar SELECT works correctly in php.

I am using: Perl v5.18.2, PostgreSQL v8.4.14

+3


source to share


1 answer


You are missing single quotes for $m_id

my $q1 = "select id from mails.mails_in where user_id=$param[5] and message_id='$m_id' and user_remote_id=$param[6]";

      

but it's always better to use ?

placeholders,



my $q1 = "select id from mails.mails_in where user_id =? and message_id =? and user_remote_id =?";
$sth = $dbh->prepare($q1);
$rv = $sth->execute($param[5], $m_id, $param[6]);

      

since you don't need to worry about quotes, escaping options and SQL injection attacks .

+6


source







All Articles