trying to use variables in <cfquery> but they don't convert to values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rjd1
    New Member
    • Feb 2014
    • 2

    trying to use variables in <cfquery> but they don't convert to values

    I'm not highly technical, and this is probably something simple, but here is

    my dilemma, where I am attempting to retrieve a single record using a variable name in the query.

    First, I define some variables:

    <cfset datasource = 'MyDatabase'>
    <cfset query_tbl = 'MyDatabaseTabl e'>
    <cfset field1 = 'actual_fieldna me1'><!--- FIRST field in MyDatabaseTable --->
    <cfset field2 = 'actual_fieldna me2'><!--- SECOND field in MyDatabaseTable --->
    <cfset ID = #Form.ID#<!--- ONE particular record I want to retrieve, passed from a form --->

    I do a test display to ensure all variables are set, and everything checks out fine.

    Then, to retrieve this single record, I run a query using those variables:

    <cfquery name="cfqGetIte m" datasource="#da tasource#">
    SELECT *
    FROM #query_tbl#
    WHERE ID = #ID#
    </cfquery>

    Then, I attempt to display the query output:

    EITHER AS

    <cfoutput>
    <p>#cfqGetItem. field1#
    </cfoutput>

    OR, AS

    <cfoutput>
    <p>#cfqGetIte m[column1_field][currentRow]#
    </cfoutput>

    In each case, I get a similar CF error message: "Element field1 is not defined in query cfqGetItem", or "Variable currentrow is not defined".

    How do I get the query output to generate the actual values for the record?

    How can a "translatio n" be done? What would be the correct way?

    Thank you very much for any help!
Working...