Getting complex object error when trying to output query values

前端 未结 1 964
太阳男子
太阳男子 2020-12-20 18:35

My goal is to just output the column data specified in the \"fieldList\".

Getting the following error:

Complex object types cannot be converte

相关标签:
1条回答
  • 2020-12-20 19:27

    Ok, I see you ammended your initial code, so here's my response:

    You are looping over a list of columns, and trying to evaluate each column as though it is a single element in a struct.

    A query, however, is slightly different: it is accessed as a series of structs, which in turn, are arrays--of each row of data as they return from the query.

    If you want to output all of the rows of data, without knowing the columns up front (or passing them in dynamically as you are implying in your code above), try this:

    <cfoutput query="myQuery">
      <cfloop list="#myQuery.ColumnList#" index="thisColumn">
        #myQuery[thisColumn][myQuery.CurrentRow]#
      </cfloop>
    </cfoutput>
    

    This will provide you with the output you need. The outer cfoutput with the query attribute will loop over all the rows of data you received. Then, for each row, you loop over the list of columns the query produces, and for each column, output the data, specifying the row via myQuery.CurrentRow, which iterates automatically for you via the outer output.

    I'd also take a moment now to advocate that you try to stay away from loops within loops, and just output your values explicitly:

    <cfoutput query="myQuery">
      #company# #phone#
    </cfoutput>
    

    Using this syntax is slightly less expensive than the aforementioned loop within a loop.

    0 讨论(0)
提交回复
热议问题