in reply to compare two values in perl

$usr is your statement handle object, not the value from the database. You need to fetch it. Further, it doesn't make a whole lot of sense to compare what you get from the database to $user_name, since you're selecting on that value anyway! Instead, I'd just check to see if the query returned anything:

my $sth = $dbh->prepare(q{SELECT user_id from user where user_id = ?}); $sth->execute( $user_name ); if( $sth->rows ) { # do something here }
If you want to get the actual value returned by the DB, you can use one of the many DBI methods for fetching results. This will put the value of the user_id column in $usr.
my ( $usr ) = $sth->fetchrow_array;

Replies are listed 'Best First'.
Re^2: compare two values in perl
by Devanchya (Beadle) on Dec 10, 2006 at 04:40 UTC
    To add my two cents into this: use fetchrow_hashref

    The simple reason is, with an array if you change the SQL statement you need to update the code. This doesn't cause an issue if your only grabbing one line... but what happens when all of a sudden want to get the first name as well so you can say hello afterwards: (note really this code would be best on a multiple reply but in this case it should 'hopefully' be one username only...)

    my $p; my ($userid,$firstname); my $sth = $dbh->prepare(q{Select user_id,firsname from user where user +_id = ?}); $sth->execute( $user_name ); while( $p = $sth->fetchrow_hashref ) { $userid = $p->{user_id}; $firstname = $p->{firstname}; } if(defined $username) { print "Hello $firstname, welcome back!"; } else { # display an error message, code somewhere else loginerror($userid); }

    --
    Even smart people are dumb in most things...