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

im trying to do a recordcount for my program so that the customer has to use a valid affiliate number which is in my database. Am i going about my code all wrong?
$RS = $Conn->Execute("SELECT * FROM affiliate where Affiliate_Number=" +.$an); $RS -> movelast; $RS -> movefirst; $RS -> Recordcount; if( $RS->Recordcount = 0 ) { &error('bad_affiliate_number'); } else { $sql="update affiliate set Affiliate_Refferals=Affiliate_Refferals+1 w +here Affiliate_Number=".$an; $Conn->Execute($sql);

Replies are listed 'Best First'.
Re: Record Count
by Stegalex (Chaplain) on Jan 18, 2002 at 22:02 UTC
    how about doing a select count(*) from affiliate where affiliate_number = '$an' if the count is zero, then there's no matching affiliate. Also, depending on your dbms, leaving the $an unquoted in your select statement may cause a syntax error if $an happens to be undefined. I like chicken.
Re: Record Count
by gav^ (Curate) on Jan 18, 2002 at 22:35 UTC
    You need to check for records returned before doing anything with the recordset.
    $RS = $Conn->Execute(... sql ...); if ($RS->EOF) # error if ($RS->Recordcount == 0) # error
    This is kinda guessing thought, I've never done ADO with Perl. You also might want to know that most datasources will always return a recordcount of 0 anyway...

    Hope this helps...

    gav^

Re: Record Count
by rbc (Curate) on Jan 18, 2002 at 23:36 UTC
    Why not just do the update?
    If the Affiliate_Number is 'bad' (not in the table)
    then nothing happens and $RS->Recordcount will be zero.

    I'd recomment creating a stored procedure on the DB
    to do this and call that stored procedure from your perl.

Re: Record Count
by JayBonci (Curate) on Jan 19, 2002 at 00:23 UTC
    Perl and ADO work pretty much the same way as other ADO functions work. Your code needs a few other things to make it work.. you may be forgetting them. (this is untested code, but it basically correct).

    Basically you need to make sure that:
    • You created the object with CreateObject. This will initialize the ADO COM objects and do all the coCreateInstance stuff you need. It seems like you had, but it's before all of this.
    • use strict; This can never hurt.
    • You called the correct connection string or DSN name in the Open method.
    That said, there are a few things that you need to check for in your code:

    $RS = $Conn->Execute("SELECT * FROM affiliate where Affiliate_Number=" +.$an); if(! $RS->BOF ) #Beginning of file, as in returned no records { $RS -> movelast; $RS -> movefirst; $RS -> Recordcount; #This may be a nitpick point, but MoveLast, MoveFirst, #and RecordCount are all case sensitive. $sql="update affiliate set Affiliate_Refferals=Affiliate_Refferals+1 w +here Affiliate_Number=".$an; }else{ &error('bad_affiliate_number'); #I don't know what you are trying to do with this, but hey. }
    Also, check on the following:
    • Does your ADO data driver for your particular DBI support the RecordCount method? I know that Access does not, and will return a -1 (if you are using really simple "home brewn databases").
    • Quote your sql string input. I CANNOT stress this enough. This is a major security problem with many many websites. If you are simply stuffing $Request->Form('form') into $an, then you are running the risk of people doing malicious input into your database, such as "blah; drop database databasename;" Seriously, this is a major problem facing small time websites. Validate your input, either with a regular expression, or whatever else it takes. Develop an input library for yourself, and maintain it well.
    I hope that clears up some of the confusion with ADO.
Re: Record Count
by seattlejohn (Deacon) on Jan 18, 2002 at 23:17 UTC
    This is a total nitpick, but for the sake of people who may have to maintain your code later, you might want to spell "referrals" correctly in your db field names. It's one f, two r's (well, three if you count the initial r). Happy coding!
Re: Record Count
by MiRaGe 508 (Novice) on Jan 18, 2002 at 22:09 UTC
    Im still gettin an error on my page. Can't call method "EOF" on an undefined value