When it comes to programming in Coldfusion I like to do things a bit different–mainly due to my background in php. For instance I hardly use cftags because I prefer to use cfscripts. This has it’s limitations, such as: no query functions exist in cfscript! The only way to execute a query is to do so before (or after) your script. Input validation can also be troublesome if you try to use cfqueryparam outside a cfquery nest. And what if you’re in the middle of a for-loop and need to query something? Thats right, your whole method has to be restructured. Until now.
I have created two wrappers that allow you to use the power of cfquery and cfqueryparam from within your cfscripts! w00t!
<cffunction name="queryParam" output="no">
<cfargument name="data">
<cfargument name="type" requried="no" default="CF_SQL_VARCHAR">
<cfargument name="max" requried="no" default="255">
<cfquery name="getFixedData" datasource="cyberSchoolDemo">
SELECT <cfqueryparam value=#arguments.data# cfsqltype=#arguments.type# maxlength=#arguments.max#> as newData
</cfquery>
<cfreturn getFixedData.newData>
</cffunction>
<cffunction name="query" access="public">
<cfargument name="queryString" required="yes">
<cfargument name="DSN" required="no" default="#application.DSN#">
<cfset tmpResultStruct = {} >
<cfquery name="builtQuery" datasource="#arguments.DSN#" result="result">
#preserveSingleQuotes(arguments.queryString)#
</cfquery>
<cfset tmpResultStruct.query = arguments.queryString>
<cfset tmpResultStruct.dataSource = arguments.DSN>
<cfset tmpResultStruct.result = result>
<cfif isdefined("builtQuery")>
<cfset tmpResultStruct.data = builtQuery>
</cfif>
<cfreturn tmpResultStruct>
</cffunction>
Example Usage:
getUser = query('SELECT * FROM users WHERE uID = #queryParam(form.userID, 'CF_SQL_INTEGER', 11)# LIMIT 1');






















