Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion Conditional RecordCount

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...

like image 234
TaylorPLM Avatar asked Dec 07 '22 19:12

TaylorPLM


1 Answers

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)

like image 157
nykash Avatar answered Dec 27 '22 22:12

nykash