64
Chapter 3: Using ColdFusion Variables
Using a query column
When you are not inside a
cfloop
,
cfoutput
, or
cfmail
tag that has a
query
attribute, you can
treat a query column as an array. However, query column references do not always behave as you
might expect. This section explains the behavior of references to query columns using the results
of the following
cfquery
tag in its examples:
<cfquery dataSource="CompanyInfo" name="myQuery">
SELECT FirstName, LastName
FROM Employee
</cfquery>
To reference elements in a query column, use the row number as an array index. For example,
both of the following lines display the word "ben":
<cfoutput> #myQuery.Firstname[1]# </cfoutput><br>
<cfoutput> #myQuery["Firstname"][1]# </cfoutput><br>
ColdFusion behavior is less straightforward, however, when you use the query column references
myQuery.Firstname and myQuery["Firstname"] without using an array index. The two reference
formats produce different results.
If you refer to myQuery.Firstname, ColdFusion automatically converts it to the first row in the
column. For example, the following lines print the word "ben":
<cfset myCol = myQuery.Firstname >
<cfoutput>#mycol#</cfoutput>
But the following lines display an error message:
<cfset myCol = myQuery.Firstname >
<cfoutput>#mycol[1]#</cfoutput><br>
If you refer to
Query["Firstname"]
, ColdFusion does not automatically convert it to the first
row of the column. For example, the following line results in an error message indicating that
ColdFusion cannot convert a complex type to a simple value:
<cfoutput> #myQuery['Firstname']# </cfoutput><br>
Similarly, the following lines print the name "marjorie", the value of the second row in the
column:
<cfset myCol = myQuery["Firstname"]>
<cfoutput>#mycol[2]#</cfoutput><br>
However, when you make an assignment that requires a simple value, ColdFusion automatically
converts the query column to the value of the first row. For example, the following lines display
the name "ben" twice:
<cfoutput> #myQuery.Firstname# </cfoutput><br>
<cfset myVar= myQuery['Firstname']>
<cfoutput> #myVar# </cfoutput><br>
Using periods in variable references
ColdFusion uses the period (.) to separate elements of a complex variable such as a structure,
query, XML document object, or external object, as in MyStruct.KeyName. A period also
separates a variable scope identifier from the variable name, as in Variables.myVariable or
CGI.HTTP_COOKIE.
Содержание COLDFUSION MX 61-DEVELOPING COLDFUSION MX
Страница 1: ...Developing ColdFusion MX Applications...
Страница 22: ...22 Contents...
Страница 38: ......
Страница 52: ...52 Chapter 2 Elements of CFML...
Страница 162: ......
Страница 218: ...218 Chapter 10 Writing and Calling User Defined Functions...
Страница 250: ...250 Chapter 11 Building and Using ColdFusion Components...
Страница 264: ...264 Chapter 12 Building Custom CFXAPI Tags...
Страница 266: ......
Страница 314: ...314 Chapter 14 Handling Errors...
Страница 344: ...344 Chapter 15 Using Persistent Data and Locking...
Страница 349: ...About user security 349...
Страница 357: ...Security scenarios 357...
Страница 370: ...370 Chapter 16 Securing Applications...
Страница 388: ...388 Chapter 17 Developing Globalized Applications...
Страница 408: ...408 Chapter 18 Debugging and Troubleshooting Applications...
Страница 410: ......
Страница 426: ...426 Chapter 19 Introduction to Databases and SQL...
Страница 476: ...476 Chapter 22 Using Query of Queries...
Страница 534: ...534 Chapter 24 Building a Search Interface...
Страница 556: ...556 Chapter 25 Using Verity Search Expressions...
Страница 558: ......
Страница 582: ...582 Chapter 26 Retrieving and Formatting Data...
Страница 668: ......
Страница 734: ...734 Chapter 32 Using Web Services...
Страница 760: ...760 Chapter 33 Integrating J2EE and Java Elements in CFML Applications...
Страница 786: ...786 Chapter 34 Integrating COM and CORBA Objects in CFML Applications...
Страница 788: ......
Страница 806: ...806 Chapter 35 Sending and Receiving E Mail...