http://qs321.pair.com?node_id=273253

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

Hi Monks,
I have written a subroutine to test if a user is in the database or not. It returns 1 if the user is in the database and 0 if the user is not in the database.
Here is the code i have written:
sub member_exists { my ($email,$mid) = @_; chomp ($email,$mid); my $db = "nMail"; my $sock = "/tmp/mysql.sock"; my $user = ""; my $pass = ''; my $dsn = "DBI:mysql:$db;mysql_socket=$sock"; my $mid = (); my $dbh = DBI->connect($dsn,$user,$pass); my $sth = $dbh->prepare("SELECT S_Email,MID FROM ML_Subscribers"); $sth->execute() || die "Error: Could not get mailing list data\n"; while (my @row = $sth->fetchrow_array) { if($row[0] eq $email and $row[1] == $mid){ return 1; } } $sth->finish(); $dbh->disconnect(); return 0; }
However this will always return 0 for all cases. When i remove  and $row[1] == $mid it works fine, but i need the $email and $mid to be the same. I've tried changing "==" to "eq" but that doesn't work either.

I have also tried the sql statement:  SELECT S_Email,MID FROM ML_Subscribers WHERE MID = '$mid' AND  S_Email = '$email' and then tested if  $row[0] is true.
None of these things worked so i am pretty stuck for ideas.
I'm sure it has to be something stupid i am over looking, i would appreciate any input.
Cheers

Neil Archibald
- /dev/IT -

Replies are listed 'Best First'.
Re: SQL - Subroutine always returning true
by gmax (Abbot) on Jul 11, 2003 at 07:17 UTC

    The reason for your routine returning 0 is that $mid was zeroed before it was tested. OK. I see that you found it.

    On a side note, I would like to point out a few things that may be source of some trouble for your code in future.

    • You are using a database connection without username and password. I assume it is just for the sake of this example. If it is not, be aware that the database security would be nil. I strongly suggest fixing it. Refere to the manual for how to do it.
    • You are connecting to a database every time you have a request. Besides being bad for performance, your routine will only call disconnect when it returns 0. The DBI will take care of this, but it would be better to move the connection/disconnection business outside. See below.
    • Your routine is calling $sth->finish() when returning 0 and just abandoning the statement handler when it returns 1. It should be the other way around. You should call finish when you don't need to process a statement handler any longer before you finished fetching records. Instead, after a complete loop, finish is called automatically by the DBI and it is not needed.
    • Last, but most important, you are doing a sequential search in the database, by fetching all the records and checking them in the client. This is the worst thing you could do for performance. Let the database do the job. Use a WHERE clause.

    Here is how I would write this routine.

    # untested sub member exists { my ($dbh, $email, $mid) = @_; my $query = qq{ SELECT COUNT(*) FROM ML_Subscribers WHERE S_Email = ? AND MID = ? }; my $sth = $dbh->prepare($query); $sth->execute ($email, $mid); my ($count) = $sth->fetchrow_array(); $sth->finish(); return $count; } my $dbh = DBI->connect("blah blah blah") or die "..."; print "Joe exists\n" if member_exists($dbh, "joe", 12); print "Fred is there\n" if member_exists($dbh, "fred", 16); # the rest of your application here. $dbh->disconnect;

    HTH

    _ _ _ _ (_|| | |(_|>< _|
      Hi,
      I did indeed remove the username and password for sake of this example

      I accidently left that ->finish() call out so thanks a lot for pointing this out, i will add it in now :)

      As i mentioned in the original question i did have a WHERE clause in there, i used this originally but changed it after i became confused with the results, once i got it working i quickly changed it back.

      Thank you for your input i really appreciate it :)

      Neil Archibald
      - /dev/IT -
      For better performance you can do following:
      sub member exists { my ($dbh, $email, $mid) = @_; my $sth = $dbh->prepare(SELECT 1 FROM ML_Subscribers WHERE S_Email +=? AND MID=?); my $res = $sth->execute($email, $mid); $sth->finish(); return $res>0 ? 1 : 0; }
            
      --------------------------------
      SV* sv_bless(SV* sv, HV* stash);
      
Re: SQL - Subroutine always returning true
by nysus (Parson) on Jul 11, 2003 at 06:25 UTC
    Throw some print statements in there to get a peek at the variables and see if they contain what you think they contain. Work backwards from there.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff";
    $nysus = $PM . $MCF;
    Click here if you love Perl Monks

      Is this unsound advice? Why is it cause me to lose reputation?

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

      Ahhh good advice, thanks a lot.

      Neil Archibald
      - /dev/IT -
One more minor item to consider.
by htoug (Deacon) on Jul 11, 2003 at 08:28 UTC
    Some databases return trailings blanks in fixed length string fields. This can cause 'eq' comparisons to fail.
    To remove the blanks, set $dbh->{ChopBlanks}=1.

    Yes, I know that this was not he problem here, but it doesn't hurt to remind people of the pitfalls given an opportunity.

    /me leaving soapbox.

Re: SQL - Subroutine always returning true
by devslashneil (Friar) on Jul 11, 2003 at 06:49 UTC
    Argh, i can't believe how simple this was!
    my $mid = ()
    That line was redeclaring $mid, i removed it and it worked fine :)

    Neil Archibald
    - /dev/IT -