Handling no query results
Your code must accommodate the cases where a query does not return any records. To determine
whether a search has retrieved records, use the
variable in a conditional logic expression that determines how to display search results
appropriately to users.
For more information on query variables, including
and Retrieving Data," on page
For example, to inform the user when no records were found by the GetEmployees query, insert
the following code before displaying the data:
<cfif GetEmployees.RecordCount IS "0">
No records match your search criteria. <BR>
<cfelse>
You must do the following:
•
Prefix
RecordCount
•
Add a procedure after the
•
Add a procedure after the
•
Follow the second procedure with a
code.
To return search results to users:
Edit the page actionpage.cfm.
1
Change the page so that it appears as follows:
2
<html>
<head>
<title>Retrieving Employee Data Based on Criteria from Form</title>
</head>
<body>
<cfquery name="GetEmployees" datasource="CompanyInfo">
SELECT Departmt.Dept_Name,
Employee.FirstName,
Employee.LastName,
Employee.StartDate,
Employee.Salary
FROM Departmt, Employee
WHERE Departmt.Dept_ID = Employee.Dept_ID
<cfif isdefined("Form.Department")>
AND Departmt.Dept_Name = <cfqueryparam value="#Form.Department#"
CFSQLType="CF_SQL_VARCHAR">
</cfif>
<cfif Form.LastName is not "">
AND Employee.LastName = <cfqueryparam value="#Form.LastName#"
CFSQLType="CF_SQL_VARCHAR">
</cfif>
</cfquery>
<cfif GetEmployees.recordcount is "0">
No records match your search criteria. <br>
Please go back to the form and try again.
<cfelse>
<h4>Employee Data Based on Criteria from Form</h4>
427.
with the query name.
tag that displays a message to the user.
cfif
tag to format the returned data.
cfelse
</cfif>
query variable. You can use the
RecordCount
, see
RecordCount
tag end to indicate the end of the conditional
Returning results to the user
Chapter 20, "Accessing
571
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers