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.