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:
cfoutput
block, once for each row returned from a database.
cfoutput
block to output the data to the page.cfoutput
block to format the data on the page.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.
empList.cfm
in ColdFusion Studio.
<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>
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.
You now display the results of the query on the page. The following table describes the code and its function:
When outputting query results, keep the following guidelines in mind:
cfquery
must precede the cfoutput
that references its results. Both must be on the same page (unless you use the cfinclude
tag).
query
attribute in the cfoutput
tag.cfoutput
block that uses a cfquery
attribute you can optionally prefix the query variables with the name of the query, for example Emplist.FirstName
.query
attribute value with double quotes ("). <br>
tag to the end of the variable references so that ColdFusion starts a new line for each row that is returned from the query.