Outputting Query Data

After you have defined a query on a page, you can use the CFOUTPUT tag with the QUERY attribute to define the query variable that you want to output to a page. When you use the QUERY attribute:

The CFOUTPUT tag accepts a variety of optional attributes but, ordinarily, you will use the QUERY attribute to define the name of an existing query.

Note To output query data on your page:
  1. Return to empList.cfm in 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 Employees
    </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 have 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.

Code Review

You now display the results of the query on the page.

Code Description
<CFOUTPUT QUERY="EmpList">
Display information retrieved in the EmpList query
#FirstName#, #LastName#, 
#Salary#, #Contract#
Display the value of the FirstName, LastName, Salary, Contract fields of the first record
<BR>
Insert a line break (go to the next line Then, keep displaying the fields you've specified for each record, followed by a line break, until you run out of records.
</CFOUTPUT>
End the CFOUTPUT block

Query Output Notes and Considerations

When outputting query results, keep these guidelines in mind: