<cfif myQuery.recordcount>
{do something}
</cfif>
<cfif myVariable>
{do something}
</cfif>
<cfif hisExpression>
{do something}
</cfif>
<cfif not yourVariable>
{do something}
</cfif>
<cfif myQuery.recordcount gt 0>
{do something}
</cfif>
<cfif myVariable gt 0>
{do something}
</cfif>
<cfif yourVariable gt 5>
{do something}
</cfif>
ColdFusion won't cause this error; you can't define a variable as NULL.
You'll never see this with Microsoft Access; it forces bit fields to zero (false).
However, you may see it if you create bad bits by hastily modifying a table in MS SQL Server or Oracle.
Suppose you've added a column to an existing table, and the column is a bit field. You probably did this interactively through whatever graphic user interface the database engine provides. You were going to force this field to a known value in your code, weren't you, when your code is eventually used to make changes to the table? However, you haven't used your code yet. So, the table has null values in bit fields. The table has bad data.
If you have a "heavy" database engine such as MS SQL or Oracle, try to demonstrate the problem. Create table BadBit with a text field Name and a bit field Status. Add a few rows through your database interface, filling in Name but leaving the Status undefined. Then run this code, placed in BadBits.cfm, against this bad data.
<cfquery name="badBitList" datasource="context"> select * from BadBit order by Name </cfquery> <table> <tr><td>NAME</td><td>STATUS</td></tr> <cfoutput query="badBitList"> </tr><td>#Name#</td> <td> <cfif Status> Awake <cfelse> Asleep </cfif> </td></tr> </cfoutput> </table>
The code will run just fine in Microsoft Access. However, it may give you an error of this form when other database engines are used:
Error Occurred While Processing Request
Error Diagnostic Information
Inside Text
Cannot convert to boolean.
Please, check the ColdFusion manual for the allowed conversions between data typesThe error occurred while processing an element with a general identifier of (CFIF)...
<cfif val(Status)> Working <cfelse> Retired </cfif> <cfif Status is 1> Up <cfelse> Down </cfif>
When you use these forms of evaluation, ColdFusion compensates for the bad data. It assumes you meant for the field to be false unless it's true.
The following code admits this, making the bad data obvious so that the user will fix it if you didn't care to [OUCH] or couldn't (because the data came from an external source). Replace the if-else-endif cluster in a copy of badbits.cfm with this code: call the result badbits3.cfm.
<cfif Status is 1> Heads <cfelseif Status is 0> Tails <cfelse> The coin landed on edge </cfif>