Let's first write the script as we would have done using a <cfquery>
tag and then see how we can convert it to the CFScript format.
1. Create a template under the
<Railo Install Directory>/webroot/Chapter_6/
namedqueryscript.cfm
and let's put the following code in there:<cfparam name="url.id" type="numeric"> <cfquery name="getUser" datasource="railobook"> SELECT * FROM Users WHERE id = <cfqueryparam cfsqltype="cf_sql_numeric" value="#url.id#"> </cfquery> <cfoutput> #getUser.username# </cfoutput>
2. In the previous code, the first line sets up a parameter called
url.id
; this means that an error will be thrown if anid
is not passed in theURL
or if it is passed and is not numeric.3. We then create our query, but instead of simply passing the variable to the query, we use
<cfqueryparam>
to say that the value we are passing is of a database typecf_sql_numeric
. Finally, we output the first item...