Record Counts of Database Results
Record Count of Database Results

This tutorial is designed to show you how to get a record count of the results returned by your database query. It’s very easy and has tons of useful applications.

First, lets pretend you have a database with demographic information for men and women living in San Deigo. Your fields are Sex, Age, Hair, EyeColor. You need to find out exactly how many females in San Deigo have blonde hair.

Now, query your database for that info.

<cfquery datasource="SanDemo" name="FemBlondes">
   Select *
   From Demographics
   Where Sex = 'Female'
       AND Hair = 'Blonde'
</cfquery>

To get the record count, simply do a
<cfoutput>#FemBlondes.recordcount#</cfoutput>

You can change the query to query anything you can think of, and the .recordcount will give you the total number of records. Along with that, you can use the cfchart tag to chart out your results.

For Example:
Same Database, multiple queries, all listed below:
<cfquery datasource="SanDemo" name="Men">
   Select *
   From Demographics
   Where Sex = 'Male'
</cfquery>

<cfquery datasource="SanDemo" name="Women">
   Select *
   From Demographics
   Where Sex = 'Female'
</cfquery>

<cfquery datasource="SanDemo" name="MenAge">
   Select *
   From Demographics
   Where Sex = 'Male'
       AND Age < '30'
</cfquery>

<cfquery datasource="SanDemo" name="WomenAge">
   Select *
   From Demographics
   Where Sex = 'Female'
       AND Age < '30'
</cfquery>

Now to chart the results in a bar graph:

<cfchart format="flash" scalefrom="1" scaleto="400" showxgridlines="no"
             showygridlines="no" showborder="no" fontbold="no" fontitalic="no"
             xaxistitle="Sex" yaxistitle="Count" show3d="yes" rotated="no"
             sortxaxis="no" showlegend="no" showmarkers="no">
                     <cfchartseries type="bar" serieslabel="San Deigo Demographics" seriescolor="##0099FF">
                     <cfchartdata item="Men" value="#Men.recordcount#">
                     <cfchartdata item="Men Under 30" value="#MenAge.recordcount#">
                     <cfchartdata item="Women" value="#Women.recordcount#">
                     <cfchartdata item="Women Under 30" value="#Women Age.recordcount#">
                     </cfchartseries>
</cfchart>

The items in bold are just the record count results of your query. This is pretty simple folks but a nice tutorial none the less.



All ColdFusion Tutorials By Author: Mike Daugherty
  • Adding Multiple Records to a Table and much more.
    Alright, this one is long at first, but once you get the hang of it, it can be really useful. Basically, we are going to create a 3 page application that will allow us to choose the number of people to add to a database, enter these peoples info onto a second page, and add all the records at one time to a database on page three.
    Author: Mike Daugherty
    Views: 10,858
    Posted Date: Wednesday, February 1, 2006
  • Integrating PayPal’s IPN with ColdFusion
    This will let a user buy something from your site using PayPal. Then you will get automatic instant notification of payment. This will also show you how to modify a database to reflect the payment and send the user a receipt.
    Author: Mike Daugherty
    Views: 11,146
    Posted Date: Thursday, July 20, 2006
  • Record Counts of Database Results
    This tutorial is designed to show you how to get a record count of the results returned by your database query. It also show you how to then chart those results.
    Author: Mike Daugherty
    Views: 9,765
    Posted Date: Friday, January 14, 2005
  • Using CFLDAP to Query or Modify Active Directory
    The CFLDAP command gives you the ability to query Active Directory to pull out (or insert) information into AD. Once place it has been useful in our organization is for creating an online directory and keeping it up to date. This can also be used to check an account against active directory for authorization
    Author: Mike Daugherty
    Views: 12,694
    Posted Date: Wednesday, January 12, 2005