Question
How can I return a dynamic column name from a query?
Heeey!!
I am stuck on this problem. I'm pretty sure it can be done. Basically, I want to have a function that returns a column from a table that the developer decided to pick. E.g. getItemField(5,"item_title") - the first being an item ID and the second being the column we want to get. This gets passed to a CFC. However, I'm having trouble on returning the value of this query column because the column name is dynamic and now in the "arguments" scope.
My code fails on the CFRETURN tag:
<cfquery name="qryGetItemField" datasource="#request.dsn#" username="#request.username#" password="#request.password#">
SELECT
<cfif trim(itemField) neq "item_id">item_id,</cfif>
#arguments.itemField#
FROM #request.tbl_items#
WHERE item_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.itemID#" />
</cfquery>
<cfif qryGetItemField.recordcount eq 1>
<!---<cfdump var="#qryGetItemField#" />--->
<cfreturn qryGetItemField["arguments.itemField"] />
<cfelse>
<cfreturn "" />
</cfif>
As you can see, I'm trying to append the query object name with the dynamic arguments name that was passed. This errors and gives the following message:
[Table (rows 1 columns ITEM_ID, ITEM_TITLE): [ITEM_ID: coldfusion.sql.QueryColumn@b1bd7e] [ITEM_TITLE: coldfusion.sql.QueryColumn@10fd4ff] ] is not indexable by arguments.itemField
The error occurred in C:\ColdFusion8\wwwroot\cms\coldbox\components\tags.cfc: line 406
Called from C:\ColdFusion8\wwwroot\cms\coldbox\index.cfm: line 90
Called from C:\ColdFusion8\wwwroot\cms\coldbox\index.cfm: line 1
Called from C:\ColdFusion8\wwwroot\cms\coldbox\index.cfm: line 1
404 : <cfif qryGetItemField.recordcount eq 1>
405 : <!---<cfdump var="#qryGetItemField#" />--->
406 : <cfreturn qryGetItemField["arguments.itemField"] />
407 : <cfelse>
408 : <cfreturn "" />
Any help would be greatly appreciated. I'm pretty sure it can be done but my syntax is probably all wrong!
Thanks,
Mikey.
I am stuck on this problem. I'm pretty sure it can be done. Basically, I want to have a function that returns a column from a table that the developer decided to pick. E.g. getItemField(5,"item_title") - the first being an item ID and the second being the column we want to get. This gets passed to a CFC. However, I'm having trouble on returning the value of this query column because the column name is dynamic and now in the "arguments" scope.
My code fails on the CFRETURN tag:
<cfquery name="qryGetItemField" datasource="#request.dsn#" username="#request.username#" password="#request.password#">
SELECT
<cfif trim(itemField) neq "item_id">item_id,</cfif>
#arguments.itemField#
FROM #request.tbl_items#
WHERE item_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.itemID#" />
</cfquery>
<cfif qryGetItemField.recordcount eq 1>
<!---<cfdump var="#qryGetItemField#" />--->
<cfreturn qryGetItemField["arguments.itemField"] />
<cfelse>
<cfreturn "" />
</cfif>
As you can see, I'm trying to append the query object name with the dynamic arguments name that was passed. This errors and gives the following message:
[Table (rows 1 columns ITEM_ID, ITEM_TITLE): [ITEM_ID: coldfusion.sql.QueryColumn@b1bd7e] [ITEM_TITLE: coldfusion.sql.QueryColumn@10fd4ff] ] is not indexable by arguments.itemField
The error occurred in C:\ColdFusion8\wwwroot\cms\coldbox\components\tags.cfc: line 406
Called from C:\ColdFusion8\wwwroot\cms\coldbox\index.cfm: line 90
Called from C:\ColdFusion8\wwwroot\cms\coldbox\index.cfm: line 1
Called from C:\ColdFusion8\wwwroot\cms\coldbox\index.cfm: line 1
404 : <cfif qryGetItemField.recordcount eq 1>
405 : <!---<cfdump var="#qryGetItemField#" />--->
406 : <cfreturn qryGetItemField["arguments.itemField"] />
407 : <cfelse>
408 : <cfreturn "" />
Any help would be greatly appreciated. I'm pretty sure it can be done but my syntax is probably all wrong!
Thanks,
Mikey.
