• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Lesson 5. Retrieve Database Information > Outputting Information from a Query

Outputting Information from a Query

After you have retrieved information from a database, you usually want to display that data. How it is presented is up to you. You can use whatever type of HTML formatting you like when outputting information from a database query; you are not stuck with a proprietary ColdFusion output look and feel. When you output information that was returned from the database, ColdFusion will loop over each record and output them one at a time, and any formatting that you have specified with these records will also be output each time. You can make the records bold, or italic, change the font or size, or put each one in a separate table cell.

In firstquery.cfm, after the output comment, type:

<cfoutput query=”productlist”> 

The <cfoutput> tag, when used with a specific query name, tells ColdFusion to output the results of a query. The query attribute specifies which query to output. The query attribute in <cfoutput> is the same as the name attribute in <cfquery>. This is how ColdFusion knows which query to output information from. In this exercise, the value of both of those attributes is productlist. You will be outputting the query you have already constructed.


Keep your query names as descriptive and short as possible. It will make it easier for you to remember what you were doing with the query.


When you use <cfoutput> with a query specified, it works like a loop; it executes the code between the two <cfoutput> tags for each record that is returned in the query.

After the <cfoutput> tag, type:

Price: #DollarFormat(ProductPrice)#

The variables that you are outputting are the ones that were returned from the productlist query. They have the same names as the fields in the database. The variables for product name, product description, and product price have been output. You will also notice that there is HTML formatting to control the line breaks and fonts.

Type </cfoutput> after the variable names.

When outputting the results of a query, the <cfoutput> tag loops over each row that was returned. (In other words, it reads each record separately.) So for each record from the database, the same code (whether it is ColdFusion or HTML) is going to be processed. The only difference is going to be the values of the variables. Knowing this, you could construct different ways to display your information: in tables, using lists, or in whatever manner you like.


There are two variables that are built into any query: recordcount and currentrow. Recordcount lets you know how many records were returned with your query. Currentrow carries the number of the row that you are currently on when looping over query results. For example, if you were on the second record in the loop, the currentrow variable would contain the number 2.

Choose File > Save As or Ctrl+Shift+S and navigate to the directory Inetpub\wwwroot\Lesson5\. Make sure the File name field says firstquery.cfm and click Save.

Open a Web browser and enter the following URL into the address window:

When you open the window, you should see a list of all the products in the database. If you have not added any more items, you should see four products. Congratulations—you have now successfully completed your first query and output that information.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint