Getting a list of columns returned by a CFQUERY tag in ColdFusion

Readability

Getting a list of columns returned by a CFQUERY tag in ColdFusion

In a pre­vi­ous exam­ple, “Deter­min­ing the num­ber of results from a data­base query in Cold­Fu­sion”, we saw how you could get the num­ber of records returned by a data­base query in Cold­Fu­sion by using the recordCount prop­erty on the query object.

The fol­low­ing exam­ple shows how you can get a list of columns in a data query object in Cold­Fu­sion by using the columnList property.


    SELECT a.ARTID, a.ARTNAME, a.DESCRIPTION, a.ISSOLD, a.LARGEIMAGE, m.MEDIATYPE, a.PRICE
    FROM ART a, MEDIA m
    WHERE a.MEDIAID = m.MEDIAID
    ORDER BY a.PRICE DESC


getArt query column list:
#getArt.columnList#

The pre­ced­ing code pro­duces the fol­low­ing output:

getArt query col­umn list:
ARTID,ARTNAME,DESCRIPTION,ISSOLD,LARGEIMAGE,MEDIATYPE,PRICE

In a previous example, “Determining the number of results from a database query in ColdFusion”, we saw how you could get the number of records returned by a database query in ColdFusion by using the recordCount property on the query object.

The following example shows how you can get a list of columns in a data query object in ColdFusion by using the columnList property.

<cfquery name="getArt" datasource="cfartgallery">
    SELECT a.ARTID, a.ARTNAME, a.DESCRIPTION, a.ISSOLD, a.LARGEIMAGE, m.MEDIATYPE, a.PRICE
    FROM ART a, MEDIA m
    WHERE a.MEDIAID = m.MEDIAID
    ORDER BY a.PRICE DESC
</cfquery>
 
getArt query column list:<br />
<cfoutput>#getArt.columnList#</cfoutput>

The preceding code produces the following output:

getArt query column list:
ARTID,ARTNAME,DESCRIPTION,ISSOLD,LARGEIMAGE,MEDIATYPE,PRICE

2 thoughts on “Getting a list of columns returned by a CFQUERY tag in ColdFusion”

  1. Thanks for the usful info. how can i get a br tag after each column name so i get a vertical list rather than a horizontal list.?

    z

    1. @ZAC,

      The columnList is just a regular ColdFusion list. You could either try looping over the list using the CFLOOP tag and output a <br/> tag, or you could try using the listChangeDelims() function to change from the default “,” list delimiter to using the <br/> as a list delimiter.
      There should be an example or two of the #listChangeDelims()# function at “Getting started with Lists in ColdFusion” #.

      Peter

Leave a Reply

Your email address will not be published.