User Guide

Table Of Contents
Outputting query data 469
Reviewing the code
The query you just created retrieves data from the cfdocexamples database. The following table
describes the highlighted code and its function:
Outputting query data
After you define a query, you can use the cfoutput tag with the query attribute to output data
from the record set. When you use the
query attribute, keep the following in mind:
ColdFusion loops through all the code contained within the cfoutput block, once for each
row in the record set returned from the database.
You must reference specific column names within the cfoutput block to output the data to
the page.
You can place text, CFML tags, and HTML tags inside or surrounding the cfoutput block to
format the data on the page.
Although you do not have to specify the query name when you refer to a query column, you
should use the query name as a prefix for best practices reasons. For example, if you specify the
Emplist query in your
cfoutput tag, you can refer to the Firstname column in the Emplist
query as Firstname. However, using the query name as a prefix—Emplist.Firstname— is
preferred, and is in the following procedure.
The
cfoutput tag accepts a variety of optional attributes but, ordinarily, you use the query
attribute to define the name of an existing query.
To output query data on your page:
1.
Edit emplist.cfm so that it appears as follows:
<html>
<head>
<title>Employee List</title>
</head>
<body>
<h1>Employee List</h1>
<cfquery name="EmpList" datasource="cfdocexamples">
SELECT FirstName, LastName, Salary, Contract
FROM Employee
</cfquery>
<cfoutput query="EmpList">
#EmpList.FirstName#, #EmpList.LastName#, #EmpList.Salary#,
#EmpList.Contract#<br>
Code Description
<cfquery name="EmpList"
datasource="cfdocexamples">
Queries the database specified in the cfdocexamples data
source.
SELECT FirstName, LastName,
Salary, Contract
FROM Employee
Gets information from the FirstName, LastName, Salary, and
Contract fields in the Employee table.
</cfquery>
Ends the cfquery block.