Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using <cfif query.recordcount> to check for retuned result

Status
Not open for further replies.

leadman

Programmer
Jun 11, 2001
177
0
0
US
Hi all,
When someone tries to log into one of my pages i run a query to check if the login/password combo exists. I've then wrapped the code that begins the logged in session inside this:

<cfif queryname.recordcount>

set session variable

</cfif>

Will that cfif statement work to stop the 'set session variable' when there is not a match in the database? The reason I ask is that I remember reading once that cfquery always returns at least one record (even if nothing matches. Does this mean that <cfif queryname.recordcount> will always return a 'TRUE'?
 
Leadman,

I think what might happen is that if there is an error with the Query it will return -1. Which is a value. You would probably be better off using:

<cfif queryname.recordcount > 0 >

or even

<cfif queryname.recordcount = 1 >
because there shouldn't be more then one match anyway for a login/password.

but that's just my opinion.
have fun...

 
thanks both, ill go do som testing now
 
leadman,

Your code is fine. You may be thinking of an anomaly when you use a aggregate function in your select clause. Try this: create a table with a numeric field in it and allow nulls in that field. Add no records, or add a couple of records and leave the field NULL. Now do &quot;SELECT max(fieldname) as highest ...&quot; You will see that although there is no maximum value, SQL return a single (blank) record. Moral of the story, when using aggregate functions, don't check for the number of records returned, instead check the value of a field in the first record. I.E.;

<cfif queryname.fieldname NEQ &quot;&quot;>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top