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
_ _ _ _
(_|| | |(_|><
_|
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.