Why won't my code display anything when my query returns nothing?
The Situation: You have executed a query and then want to display the results of that query using <cfoutput>. All works well -- unless your query doesn't return any rows. Then, nothing displays.
Computers are incorporated in modern ice cream vending machines to enhance their functionality. Ice Cream Vending machines are manufactured by many companies. Your competition will try to overcome all requests for high-tech ice cream vending machines and credit card acceptors
|
|
The Solution: When used with the "query=" parameter, <CFOUTPUT> acts as a loop, iterating through each row returned by the query. If you don't have any rows returned, it will skip over ALL the contents between the <cfoutput></cfoutput> tags so that if the query below returns no rows, the output on the screen will be blank.
<cfquery datasource="aDatasoruce" name="myQ">
SELECT userID, userName
FROM Users
WHERE fullname = 'Hal Helms'
</cfquery>
<cfoutput query="myQ">
Your user ID number is #userID#
</cfoutput>
If the query "myQ" did not return at least one row, the code inside the <cfoutput> will never evaluate. To prevent this, you want to test the RECORDCOUNT property of the returned query.
<cfif myQ.recordcount GT 0>
<cfoutput query="myQ">
Your user ID number is #userID#
</cfoutput>
<cfelse>
Sorry, we couldn't find you in our database.
</cfif>
|