in reply to SQL - Subroutine always returning true

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.

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

_ _ _ _ (_|| | |(_|>< _|

Replies are listed 'Best First'.
Re: Re: SQL - Subroutine always returning true
by devslashneil (Friar) on Jul 11, 2003 at 07:32 UTC
    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 -
Re: Re: SQL - Subroutine always returning true
by nite_man (Deacon) on Jul 11, 2003 at 11:58 UTC
    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);