In my Database Design class last night, the use of ColdFusion was introduced to the other students, and I posted the following message on the internal forum. I'm reposting here in case it helps anyone else new to ColdFusion.

--

Just to clarify the use of CFOUTPUT in the 11/15 class...

There were two examples of CFOUTPUT operating on a query result set in the handout.

In the earlier example it was demonstrated that a query which returned only one row could be output by nesting references to the queryname.column inside a CFOUTPUT tag. When CFOUTPUT is used without any attributes in the opening tag its behavior is to just evaluate ColdFusion variables to their values that are between it and the closing CFOUTPUT tag. So I could perform a simple addition such as [CFSET sum = 1 + 2> followed by [CFOUTPUT>#sum#[/CFOUTPUT> and the resulting output would be 3. This output gets added to the generated content buffer for the HTTP Response and eventually gets sent back to the browser for display.

So Maria knew that the query would only return one row, so when she used a 'plain' CFOUTPUT tag to evaluate query.column she got the value for the first and only row. If a query result set contains more than one row then the CFOUTPUT behavior is to evaluate query.column as the first row only, and this type of output would have ignored any other rows returned from the query.

However, when CFOUTPUT is used with one of the optional attributes query then CFOUTPUT will act as an iterator and loop once for every record contained in the query object (result set). For example the following would result in the value for each row of that column being output followed by an HTML break tag, so the output might appear like a vertical list.

view plain print about
1[CFOUTPUT query="myQuery">#myQuery.myColumn#<br/>[/CFOUTPUT>


Moreover, within the context of a CFOUTPUT tag using the query="" attribute, the default scope for any variable is the query scope being iterated over. So it is actually redundant to use myQuery.myColumn and instead the current row can be referred to as simply myColumn:
view plain print about
1[CFOUTPUT query="myQuery">#myColumn#<br/>[/CFOUTPUT>


By now you've figured out that the # characters are used to denote variable that should be evaluated. The # is sometimes referred to as an octothorpe or pound sign. More on that can be found here.

ColdFusion can hold variables in many scopes, some of which are local to the page request, some are shared across a users session, some are across a whole application for all requests and all sessions. It is often good practice to explicity use the scope of a variable to remove confusion and make code easier to read. Consider the following:

view plain print about
11 [cfset name = "steve">
22 [cfoutput>#name#[/cfoutput>
33 [cfoutput query="employees">
44 #name# - #variables.name#<br/>
55 [cfoutput>


This would output something like this:
view plain print about
1steve
2bill - steve<br/>
3sue - steve<br/>
4tom - steve</br>


The unscoped variable #name# on line 2 would be resolved to the 'variables' scope, and the #name# on line 4 would be first resolved to the query scope since it is the first scope checked when inside CFOUTPUT query="" tag, and #variables.name# also on line 4 would directly access the 'variables' scope and not the query scope, resulting in bill - steve or sue - steve.

Among ColdFusion developers the terms query object and result set are used synonomously to refer to the records returned from a CFQUERY or CFSTOREDPROC tag, although other groups of information in ColdFusion are also referred to as a query object such as the records returned from a CFLDAP call to look up users or the message list headers returned from a call to a mail (POP3) server with CFPOP.

A specific row in a CFQUERY result set can be accessed with array syntax as well, such as myQuery.myColumn[3] to get just the 3rd row of the query result.

Finally, a very easy way to immediately see the entire CFQUERY result set in a color-coded table format is to use the CFDUMP tag like this: [CFDUMP var="#myQuery#">

This is useful during debugging when you don't want to mess with a HTML table or break tags.