Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

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

In reply to Re: SQL - Subroutine always returning true by gmax
in thread SQL - Subroutine always returning true by devslashneil

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-19 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found