Using CFOUTPUT to Output Query Data

Use the CFOUTPUT tag 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, most frequently, you will use the QUERY attribute to define the name of an existing query.

Refer to the CFML Language Reference for ColdFusion Express for full CFOUTPUT syntax.

CFOUTPUT usage example

The code below outputs the data contained in the FirstName, LastName, StartDate, and Contract columns of the EmpList query - a query performed earlier on the same page:

<CFOUTPUT QUERY="EmpList">
    #FirstName# #LastName# #StartDate# #Salary# #Contract#<BR>
</CFOUTPUT>
Note Note:You will learn how to use an HTML table to format the output Chapter 6, Formatting and Manipulating Data.

A query name must exist on the page in order to successfully output its data.

Note To output query data on your page:
  1. Return to EmpList.cfm in HomeSite.
  2. Add this code below the CFQUERY block to define the QUERY to output:
  3. <CFOUTPUT QUERY="EmpList">
    </CFOUTPUT>
    
  4. Within the CFOUTPUT block, reference the columns that you want to output. Add a <BR> tag to format the results.
  5. #FirstName# #LastName# #StartDate# #Salary# #Contract#<BR>
    
  6. Save your changes.
  7. Your page should look like this:

    <HTML>
    <HEAD>
    <TITLE>Employee List</TITLE>
    </HEAD>
    <BODY>
    <H1>Employee List</H1>
    <CFQUERY NAME="EmpList" DATASOURCE="HRExpress">
        SELECT FirstName,LastName, StartDate, Salary, Contract
        FROM Employees
    </CFQUERY>
    <CFOUTPUT QUERY="EmpList">
        #FirstName# #LastName# #StartDate# #Salary# #Contract#<BR>
    </CFOUTPUT>
    </BODY>
    </HTML>
    
  8. View the page in a browser.
  9. A client list displays in the browser.

    Each line displays one row of data.

Click here to see how the page should look at this time.

Click here to see the code behind the scenes.

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.