Outputting Query Data

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:

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. Return to empList.cfm in ColdFusion 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 Employee
    
    </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 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.

Reviewing the code

You now display the results of the query on the page. The following table describes the code and its function:
Code
Description

<cfoutput query="EmpList"> 

Display information retrieved in the EmpList query. Display information for each record in the query, until you run out of records.

#FirstName#, #LastName#, #Salary#, #Contract# 

Display the value of the FirstName, LastName, Salary, Contract fields of each record, separated by commas and spaces.

<br> 

Insert a line break (go to the next line) after each record.

</cfoutput> 

End the cfoutput block.

Query output notes and considerations

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



Banner.Novgorod.Ru