Determining the number of results from a database query in ColdFusion

Readability

Determining the number of results from a database query in ColdFusion

In a pre­vi­ous exam­ple, “Query­ing a data­base in Cold­Fu­sion using the CFQUERY tag”, we saw how you could query a data­base in Cold­Fu­sion 9 using the <CFQUERY> tag.

The fol­low­ing exam­ple shows how you can 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.


    SELECT *
    FROM ART


#getArt.recordCount# records found

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

55 records found

If you only want the num­ber of items in a query, you could also use the SQL COUNT() method, as seen in the fol­low­ing example:


    SELECT COUNT(*) AS numItems
    FROM ART


#getArt.numItems# records found

In a previous example, “Querying a database in ColdFusion using the CFQUERY tag”, we saw how you could query a database in ColdFusion 9 using the <CFQUERY> tag.

The following example shows how you can get the number of records returned by a database query in ColdFusion by using the recordCount property on the query object.

<cfquery name="getArt" datasource="cfartgallery">
    SELECT *
    FROM ART
</cfquery>
 
<cfoutput>#getArt.recordCount# records found</cfoutput>

The preceding code produces the following output:

55 records found

If you only want the number of items in a query, you could also use the SQL COUNT() method, as seen in the following example:

<cfquery name="getArt" datasource="cfartgallery">
    SELECT COUNT(*) AS numItems
    FROM ART
</cfquery>
 
<cfoutput>#getArt.numItems# records found</cfoutput>

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>