Programming instructions
Using a web page to list trips 65
the Trip List page presented earlier in this lesson. In this example, you use cfquery to
return all the trip names found in the tripName column within the Compass Travel Trips
table. To use the SQL SELECT statement to dynamically retrieve this information, you
must execute the SQL SELECT statement between the
cfquery start and end tags as
follows:
<cfquery name="TripResult" datasource= "CompassTravel">
SELECT tripName FROM trips
</cfquery>
Displaying the query result using cfoutput
In Chapter 2, you learned that the ColdFusion
cfoutput tag is an easy mechanism to
display literal text and the contents of variables. Additionally, the
cfoutput tag
significantly simplifies displaying the results of queries. When used to display the results
from a query, the
cfoutput tag automatically loops through the record set for you. You
simply specify the name of the query in the QUERY attribute of the
cfoutput tag:
<cfoutput query="TripResult">
All the code between the cfoutput start and end tags is the output code block. The
output code block executes repeatedly, once for each row in the record set. However, if
the query returns no rows, ColdFusion skips the code contained in the output code
block.
<cfoutput query = "xxx">
...output code block...
</cfoutput>
Displaying the column contents from the SQL statement
In CFML you surround variables with pound signs (#) to display their contents using the
cfoutput tag. You also use this approach with column names specified in the SELECT
statement of a
cfquery. For instance, when you want to display the trip names from the
SQL query, you would simply use
#tripName# within the output code block.
<cfoutput query="TripResult">
#tripname#
</cfoutput>
For additional information about using SQL with cfquery and cfoutput, see
Developing ColdFusion MX Applications with CFML.