30
Chapter 3 Querying a Database
Outputting Query Data
After you define a query on a page, you can use the
cfoutput
tag with the
query
attribute to specify the query object that contains the data you want to output to a
page. When you use the
query
attribute:
•
ColdFusion loops over all the code contained within the
cfoutput
block, once for
each row returned from a 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.
•
You do not have to specify the query object name when you refer to a query
column. For example, if you specify the Emplist query in your
cfoutput
tag, you
can refer to the Firstname column in the Emplist query as either
Emplist.Firstname or just Firstname.
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
Return to
empList.cfm
in ColdFusion Studio.
2
Edit the file so that it appears as follows:
<html>
<head>
<title>Employee List</title>
</head>
<body>
<h1>Employee List</h1>
<cfquery name="EmpList" datasource="CompanyInfo">
SELECT FirstName, LastName, Salary, Contract
FROM Employee
</cfquery>
<cfoutput query="EmpList">
#FirstName#, #LastName#, #Salary#, #Contract#<br>
</cfoutput>
</body>
</html>
3
Save the file as emplist.cfm.
4
View the page in a browser.
A list of employees appears in the browser, with each line displaying one row of
data.
You created a ColdFusion application page that retrieves and displays data from a
database. At present, the output is raw. You will learn how to format the data in the
next chapter.
Reviewing the code
Summary of Contents for COLDFUSION 5-DEVELOPING
Page 1: ...Macromedia Incorporated Developing ColdFusion Applications MacroMedia ColdFusion 5 ...
Page 58: ...38 Chapter 3 Querying a Database ...
Page 134: ...114 Chapter 7 Updating Your Database ...
Page 210: ...190 Chapter 10 Reusing Code ...
Page 232: ...212 Chapter 11 Preventing and Handling Errors ...
Page 238: ...218 Chapter 12 Using the Application Framework ...
Page 262: ...242 Chapter 12 Using the Application Framework ...
Page 278: ...258 Chapter 13 Extending ColdFusion Pages with CFML Scripting ...
Page 320: ...300 Chapter 15 Indexing and Searching Data ...
Page 336: ...316 Chapter 16 Sending and Receiving E mail ...
Page 374: ...354 Chapter 18 Interacting with Remote Servers ...