User Guide

Validating Data Types 95
The following example shows the use of cfqueryparam when valid input is given in
the Course_ID variable used as a query parameter. To see what happens when you
use invalid data, substitute a text string such as test for the integer 12 in the
cfset
statement.
Note that this example uses the cfsnippets database that is provided with
ColdFusion, not the CompanyInfo database used in most of this book.
<html>
<head>
<title>cfqueryparam Example</title>
</head>
<body>
<h3>cfqueryparam Example</h3>
<cfset course_id=12>
<cfquery name="getFirst" datasource="cfsnippets">
SELECT *
FROM courses
WHERE Course_ID=<cfqueryparam value="#Course_ID#"
cfsqltype="CF_SQL_INTEGER">
</cfquery>
<cfoutput query="getFirst">
<p>
Course Number: #number#<br>
Description: #descript#
</p>
</cfoutput>
</body>
</html>
Reviewing the code
The following table describes the code and its function:
Code Description
<cfset Course_ID=12>>
Set the course_ID variable to 12.
<cfquery name="getFirst"
DataSource="cfsnippets">
Query the cfsnippets data source and
return the results in the getFirst query
object.
SELECT *
FROM courses
Select all columns from the courses
table.