Outputting query data

After you define a query on a page, you can use the cfoutput tag with the query attribute to output data from the record set to a page. When you use the query attribute, keep the following in mind:

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

To output query data on your page:

  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="CompanyInfo">
      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:

    Query output from the cfoutput tag in a browser.

    A list of employees appears in the browser, with each line displaying one row of data.

    Note:   You might need to refresh your browser to see your changes.

You created a ColdFusion application page that retrieves and displays data from a database. At present, the output is raw and needs formatting. For more information, see "Retrieving and Formatting Data".

Reviewing the code

The results of the query appear on the page. The following table describes the highlighted code and its function:
Code
Description
<cfoutput query="EmpList">
Displays information retrieved in the EmpList query.
#EmpList.FirstName#, #EmpList.LastName#,
#EmpList.Salary#, #EmpList.Contract#
Displays the value of the FirstName, LastName, Salary, and Contract fields of each record, separated by commas and spaces.
<br>
Inserts a line break (go to the next line) after each record.
</cfoutput>
Ends the cfoutput block.

Query output notes and considerations

When outputting query results, keep the following guidelines in mind:

Comments