Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I want to compare two values in a cgi script. One is a string passed from a form to the cgi script, and the other is a string value taken from a mysql database. This is the code i am using to compare the two values
my $usr; $usr = $dbh->prepare(q{SELECT user_id from user where user_id = ?}); $usr->execute($user_name); if($user_name eq $usr) { #Do something here }
In the above i am trying to pass a value from the 'user' table into the variable $usr, and then compare it to the value in $user_name. The problem is that the value in $usr is not a string as expected, when i print it to the screen it appears as DBI::st=HASH(0x83d7068) Can anybody tell me why this happens? And if there is anyway to compare these two values. Thanks

Replies are listed 'Best First'.
Re: compare two values in perl
by friedo (Prior) on Dec 09, 2006 at 21:23 UTC

    $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;
      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...
Re: compare two values in perl
by madbombX (Hermit) on Dec 09, 2006 at 21:26 UTC
    Once you execute, you still have to fetch your result set. So the following should work better for you:
    my $usr = $dbh->prepare(q{SELECT user_id from user where user_id = ?} +) or die("usr prepare error: $!"); $usr->execute($user_name) or die("usr execute error: $!"); while (@row = $usr->fetchrow_array ) { if ($user_name eq $row[0]) { # Blah } }

    You should also be checking the return values to ensure your statements are being handled properly.

      Thanks very much for all the advice, it was a great help i now have it working, thanks again