I am currently setting up a system to retrieve data from my ColdFusion database using JavaScript. However, I am encountering an error and unsure of its cause since I am relatively new to CF. The specific engine I am utilizing is ColdFusion MX 7.
Below is the content of the CFC file named cfquery.cfc
:
<cfcomponent output="no">
<cffunction name="phonebookQuery" access="remote" returnType="struct">
<cfargument name="select" type="string" required="yes">
<cfargument name="from" type="string" required="yes">
<cfargument name="where" type="string" required="yes">
<cfargument name="orderBy" type="string" required="yes">
<cfquery name="query" datasource="phonebook">
SELECT #select#
FROM #from#
WHERE #where#
ORDER BY #orderBy#
</cfquery>
<cfreturn query>
</cffunction>
</cfcomponent>
It seems that I might not be returning values correctly as my code fails to execute beyond a certain point.
Presented below is the JS function responsible for making an AJAX call. Although I acknowledge that using async: false
is not best practice, I am opting for temporary functionality until the main implementation stage where promises or callbacks will come into play.
function cfQuery(p){
var result;
var queryStr= "cfquery.cfc?method=phonebookQuery&" +
"select="+p.select+"&"+
"from="+p.from+"&"+
"where="+p.where.replace("=","%3D")+"&"+
"orderBy="+p.orderBy;
$.get( queryStr, function(data){
result=data;
});
return result;
}
The code snippet below showcases a specific function call example I am trying to rectify:
var query_result= cfQuery({
select: "*",
from: "shareloantypes",
where: "share_loan='S'",
orderBy: "share_loan_type"
});
A server error occurs whenever I run the above code. It indicates a syntax problem near the 'S' in the SQL statement:
Error Executing Database Query.
[Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC SQL Server Driver][SQL Server]
Line 1: Incorrect syntax near 'S'.
The error occurred in C:\Inetpub\wwwroot\xxxxxx\report\cfquery.cfc: line 12
10 : FROM #from#
11 : WHERE #where#
12 : ORDER BY #orderBy#
13 : </cfquery>
14 : <cfreturn query>
SQL SELECT * FROM shareloantypes WHERE share_loan=''S'' ORDER BY share_loan_type
DATASOURCE phonebook
VENDORERRORCODE 170
SQLSTATE 42000
My issue lies with the transformation of 'S' into ''S''. How can this be resolved? When replacing where: "share_loan='S'"
with where: "share_loan=S"
within the function call, the generated SQL omits quotes entirely leading to an "invalid column name" error.