
413
DEVELOPING
COLDFUSION 9 APPLICATIONS
Accessing and Using Data
Las
t
upda
te
d 8/5/2010
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 recordset.
When you use the
query
attribute, keep in mind the following:
•
ColdFusion loops through all the code contained within the
cfoutput
block, once for each row in the recordset
returned from the database.
•
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, 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.
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>
</cfoutput>
</body>
</html>
2
Save the file and view it in your web browser:
A list of employees appears in the browser, with each line displaying one row of data.
Note:
If necessary, refresh your browser to see your changes.
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.
Содержание COLDFUSION 9
Страница 1: ...Developing Applications ADOBE COLDFUSION 9...