[thelist] ColdFusion query questions

Pete Freitag pf at cfdev.com
Wed Dec 3 17:10:45 CST 2003

Kelly Hallman wrote:

>Just a couple of quick/easy CF questions (long story, but I'm hacking a CF 
>version of another web app very quickly, and I don't know CF)...
>I understand how to run a query with CFQUERY, and how to output the 
>results with CFOUTPUT ... how do I check/manipulate the results, and/or 
>know if there were any results, or if there was a query error?
>For instance, if I want to SELECT and see if there was any matching rows?  
>How do I loop/test them without outputting them? What if I did an INSERT
>that failed due to a table constraint, how do I test for that?
>By the way, this is CF version 5 on Linux.
>Thank you very much!!
Hi Kelly,

To check and see if the query returned any results you can use the 
variable yourqueryname.recordcount, so something like this would see if 
no results were returned:
<cfif yourquery.recordcount eq 0>
    Sorry no results.

If the query generates an error an exception is thrown, if the exception 
is not caught then the error message is displayed, or you can catch the 
error like this:

    <cfquery > ... </cfquery>
    <cfcatch type="database">
       There was an error in your database query: <cfdump var="#cfcatch#">

Pete Freitag
Author of the CFMX Developers Cookbook

More information about the thelist mailing list