Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: SQL - Subroutine always returning true

by gmax (Abbot)
on Jul 11, 2003 at 07:17 UTC ( [id://273271]=note: print w/replies, xml ) Need Help??


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.

  • 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

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

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);
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://273271]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found