Simon Baynes: Web Development

cfquery variables

When you run a select query with cfquery ColdFusion creates 3 variables for you automatically. These are a list of the column names, the total number of records and the current record. They are as follows:-

queryname.CurrentRow  
queryname.RecordCount  
queryname.ColumnList  

queryname.CurrentRow can only be used in a loop so either when you are using cfoutput or cfloop with the query attribute. It returns the current row as the name suggests.

queryname.RecordCount will return the number of records in a query.

queryname.ColumnList will return a comma separated list of the column names in the query. They will be in alphabetical order and not in the order that they appear on the database.

You will find all of these useful especially the first two as they provide you with invaluable information. I find the column list particularly useful when I am getting just one record from the database, as I can dynamically create a structure to hold all the information.

<!--- run query --->
<cfquery name="qGetData" datasource="data">
    SELECT *
    FROM Members
    WHERE MemID = 1
</cfquery>

<!--- create sturcture to hold user data --->
<cfset MemData = StructNew()>

<!--- loop through data to create structure --->
<cfloop list="#qGetData.columnList#" index="heading" delimiters=",">
    <cfset "MemData.#heading#" = qGetData[#heading#]>
</cfloop>

<!--- display structure --->
<cfdump var="#MemData#">
By Simon Baynes