Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for a null value from the return value in ColdFusion query loop

<cfloop query="GET_ALL_STUDENTS>
 <cfif #student_id# is  NOT NULL>
 <!--- do something--->
 </cfif>
</cfloop>   

Above is how I am looping my cf query which returns null value and I want to check if the student_id is null or not. This is what I have tried and it failed. Can anyone tell me a better way?

like image 547
Evlikosh Dawark Avatar asked Feb 09 '12 05:02

Evlikosh Dawark


People also ask

How do you check null values in ColdFusion?

QueryTable) will return empty string for any null value, it's parent class coldfusion. sql. Table is providing a method getField(row, column) to access the query table values directly, which return "undefined" if the value is null. We can make use of the IsNull to identify the "undefined" hence able to detect NULL.

How do you set NULL values in ColdFusion?

Null settings at server levelIn the ColdFusion Administrator, click Server Settings > Settings. In the page, you there is the option Enable Null Support. If you enable this option, all your applications running in the server becomes null-enabled. You can also update this setting through an Admin API.


3 Answers

You can use your database's ifNull() or the like. However, in ColdFusion, queries are returned as strings. Given your situation, easiest way is to check for a non-empty string:

<cfif len(student_id)>

By the way, you don't need the pound signs inside of an evaluation: only when using a variable as a literal (such as when outputting)

like image 150
Billy Cravens Avatar answered Nov 02 '22 23:11

Billy Cravens


In Adobe ColdFusion 9, you can do:

<cfif IsNull(student_id)>
</cfif>

Or since you're doing the opposite:

<cfif NOT IsNull(student_id)>
</cfif>
like image 37
NullyB Avatar answered Nov 02 '22 23:11

NullyB


It looks like the query is retrieving all of the students and then cfloops over the records to find the student_id fields that are NULL.

It would be more efficient to write a query that specifically queried the records that have student_id IS NULL.

The method of grabbing all the student table records will work great when you have 100 or so students. What happens when it is put into production and there are 25,000 students?

like image 41
Scott Jibben Avatar answered Nov 03 '22 00:11

Scott Jibben