# 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:

- ColdFusion loops over all the code contained within the CFOUTPUT block, once for each row returned from a database.
- Reference specific column names within the CFOUTPUT block to output the data to the page.
- You can place text and HTML tags inside or surrounding the CFOUTPUT block to format the data on the page.

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.

| 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:

- Run a CFQUERY before referencing its results using a CFOUTPUT with a QUERY attribute.
- It's a good idea to run all queries before all output blocks.
- A query name must exist on the page in order to successfully output its data.
- Surround variable references with pound signs to output their current values to a page.
- Prefix variables with their variable type -- in the case of a query variable, it's the name of the query.
- When outputting the data itself, you define the variable name using the QUERY attribute.
- When outputting query properties variables, don't use the QUERY attribute; instead, prefix the variable reference with the name of the query, for example, `EmpList.RecordCount`.
- Columns must exist and be retrieved to the application in order to output their values.
- As with other attributes, surround the QUERY value with double quotes (").
- As with any variables that you reference for output, surround column names with pound signs (#) to tell ColdFusion to output the column's current values.
- Add a <BR> tag to the end of the variable references so that ColdFusion will start a new line for each row that is returned from the query.