Copy link to clipboard
Copied
Hi:
Here is my coldfusion code:
<cfquery name = "query" datasource = "cfmysql" result = "result">
SELECT COUNT(*) FROM students WHERE gender = 'boy'
</cfquery>
And then I want to check if value is greater than 0. I then do:
<cfif existSearchHintsQuery.COUNT(*) gt 0>
But coldfusion gives me an error at the * character, the colume of the mysql result is "COUNT(*)", so I really need the * character.
How do I fetch that count number?
Thanks.
Copy link to clipboard
Copied
You need to use an alias so you can access the COUNT(*) like any other column:
<cfquery name="yourQueryName" datasource="cfmysql" result="result">
SELECT COUNT(*) AS TotalStudents FROM students WHERE gender = 'boy'
</cfquery>
<cfif yourQueryName.totalStudents GT 0>
... do something ...
</cfif>
Also, read up on using cfqueryparam (if you are not using it already)
http://livedocs.adobe.com/coldfusion/8/Tags_p-q_18.html
Copy link to clipboard
Copied
Totally correct answer, thanks.