Error in my CFQUERYPARAM
-
26-04-2021 - |
Question
I'm sure I'm just doing this horribly wrong with CFQUERYPARAM, but here's what I'm trying to do:
<CFQUERY DATASOURCE="tr3" NAME="qryPartner">
SELECT *
FROM UsrMatchActualTR2
WHERE
session = #userSess#
AND
user_id = #userID#
AND
site=<cfqueryparam value="#userSite#" cfsqltype="CF_SQL_VARCHAR" maxlength="5">
</CFQUERY>
And I'm receiving the error
Error Executing Database Query.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND site='UTD'' at line 3
Solution
I suspect your userID variable is blank, causing a "hole" in your query structure. You should really use CFQUERYPARAM for all of your arguments, including userID and userSess.
OTHER TIPS
First, make sure your datatypes are correct, is the site column a varchar / string? Also, try to use cfqueryparam for all variables.
<CFQUERY DATASOURCE="tr3" NAME="qryPartner">
SELECT * FROM UsrMatchActualTR2
WHERE session = <cfqueryparam value="#userSess#" cfsqltype="cf_sql_varchar" />
AND user_id = <cfqueryparam value="#userID#" cfsqltype="cf_sql_integer" />
AND site = <cfqueryparam value="#userSite#" cfsqltype="cf_sql_varchar" maxlength="5" />
</CFQUERY>
Try replacing the "double quotes" around the value param
WHERE session = #userSess# AND user_id = #userID# AND site=<cfqueryparam value="#userSite#" cfsqltype="CF_SQL_VARCHAR" maxlength="5">
with 'single quotes'
WHERE session = #userSess# AND user_id = #userID# AND site=<cfqueryparam value='#userSite#' cfsqltype="CF_SQL_VARCHAR" maxlength="5">
I have seen CF differentiate. Also, you should use <cfqueryparam>
for all of your variables. If you still have trouble, try to evaluate what is stored in the variables userSess, userID and site using <cfdump> and <cfabort>