4

Alright SO users... Here's a seemingly impossible to go wrong conditional statement. It is very simple, however, I cannot figure out why it won't work the way it is expected to.

<cfoutput query="checkForAd">
        <!--- also used the line <cfif RecordCount eq 0> --->
    <cfif checkForAd.RecordCount eq 0>
        <!--- Display some message. (Perhaps using a table, undecided) --->
    <cfelse>
        <!--- Display some other message. (Happens to be a table) --->
    </cfif>
</cfoutput>

When the RecordCount returns a number greater than 0, the else case displays properly. When RecordCount returns 0, nothing is displayed and the form continues along its path. I've become very frustrated as this should be quite simple...

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
TaylorPLM
  • 101
  • 1
  • 1
  • 10

2 Answers2

13

The output won't return any results if the query set is empty. Try:

<cfif checkForAd.RecordCount eq 0>
    <!--- Display some message. (Perhaps using a table, undecided) --->
<cfelse>
    <cfoutput query="checkForAd">
        <!--- Display some other message. (Happens to be a table) --->
    </cfoutput>
</cfif>

I assume that you're looking to return a number of records... if you were just returning one, the query="checkForAd" isn't necessary. You can simply reference the query & variables in a <cfoutput></cfoutput>.

Edit

Here's one way to access a query variable: QueryName["ColumnName"][RowNum]

(As you look to expand your coding, there's a lot you can do with query variables. There's a great rundown of the different approaches at ColdFusion and getting data from MySQL)

Community
  • 1
  • 1
nykash
  • 447
  • 5
  • 16
  • Thanks very much. I actually am only returning one record, however I'm quite new to ColdFusion, as well as production development in general. Thanks for the other tip. :) – TaylorPLM Sep 26 '11 at 21:31
  • My pleasure... feel free to accept the answer. It will help with future questions. – nykash Sep 26 '11 at 21:34
  • I shall, I'm simply waiting for the time limit on accepting one to be up. – TaylorPLM Sep 26 '11 at 21:35
  • cool, thx. By the way, I just posted up on edit to point you in the right direction with accessing query vars. – nykash Sep 26 '11 at 21:40
  • It's worth pointing out that RowNum generally defaults to 1, so you can simply to `QueryName["ColumnName"]` (and also `QueryName.ColumnName` if there are no special characters in the column name). Though also note that if you have looped through a query (or are adding new records) then the row num will be the _last_ record not the first one. – Peter Boughton Sep 26 '11 at 21:53
  • I've had a few instances where the rownum has to be forced for one reason or another... or it could be the obj-c coding is forcing me to put index numbers everywhere:-) – nykash Sep 26 '11 at 21:58
  • I think there might be issues if you have nested loops (for different queries), or something along those lines, but if it is a simple one-row query with nothing else involved then I've not encountered a problem accessing directly. I'm so glad I've never needed to do Objective C; it looks hideous. :) – Peter Boughton Sep 26 '11 at 22:04
3

As nykash has pointed out, the body of a cfoutput query (or a cfloop query) never executes if there are no records, so doing a check for a zero recordcount will never be true inside of one.

However, I find the following example a more readable one:

<cfif NOT checkForAd.RecordCount >
    <!--- Display some message. --->
</cfif>

<cfoutput query="checkForAd">
        <!--- loop through data --->
</cfoutput>

It might not seem much in isolation, but I think it's a bit cleaner and easier to see what's going on, especially when combined with other code.

Specifically on the RecordCount check, if I care about the specific number then I'll use EQ (or NEQ/GT/etc) but if I only care about "having records" vs "not having records" then I use the implicit boolean conversion that CFML provides to simplify the code. This really makes it easier to identify when I'm using the common binary choice or a more significant one, so navigating through the code is easier.

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176