Getting Information About Query Results

Each time you query a database with the cfquery tag, you get not only the data itself, but also query properties, as described in the following table:
Property
Description
RecordCount
The total number of records returned by the query.
ColumnList
A comma-delimited list of the query columns.
CurrentRow
The current row of the query being processed by cfoutput.

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>
    <br>
    <cfoutput>
      The query returned #EmpList.RecordCount# records.
    </cfoutput>
    </body>
    </html>
    
  3. Save the file as emplist.cfm.
  4. View the page in a browser.

The number of employees now appears below the list of employees.


Note

The variable cfquery.executionTime contains the amount of time, in milliseconds, it took for the query to complete. Do not prefix the variable name with the query name.


Reviewing the code

You now display the number of records retrieved in the query. The following table describes the code and its function:
Code
Description
<cfoutput> 
Display what follows
The query returned 
Display the text "The query returned"
#EmpList.RecordCount# 
Display the number of records retrieved in the EmpList query
records 
Display the text "records"
</cfoutput> 
End the cfoutput block.

Query properties notes and considerations

When using query properties, keep the following guidelines in mind:

  • Reference the query property within a cfoutput block so that ColdFusion outputs the query property value to the page.
  • Surround the query property reference with pound signs (#) so that ColdFusion knows to replace the property name with its current value.
  • Do not use the cfoutput tag query attribute when you output the RecordCount or ColumnList property. If you do, you will get one copy of the output for each row. Instead, prefix the property with the name of the query.