尝试输出查询值时遇到复杂的对象错误 [英] Getting complex object error when trying to output query values

查看:74
本文介绍了尝试输出查询值时遇到复杂的对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是仅输出在 fieldList中指定的列数据。

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

出现以下错误:


不能将复杂的对象类型转换为简单值。表达式已请求将变量或中间表达式结果作为简单值,但是,结果无法转换为简单值。简单值是字符串,数字,布尔值和日期/时间值。查询,数组和COM对象是复杂值的示例。该错误的最可能原因是您试图将复杂值用作简单值。例如,您可能正在尝试在cfif标记中使用查询变量。错误发生在第20行。

Complex object types cannot be converted to simple values. The expression has requested a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values. The most likely cause of the error is that you are trying to use a complex value as a simple one. For example, you might be trying to use a query variable in a cfif tag. The error occurred on line 20.

尝试执行以下操作时:

<cfquery datasource="retailers" name="myQuery">
Select * FROM retailer 
WHERE retailer_id = '#url.id#'
</cfquery>

<cfset fieldList = "company,phone,phone_secondary,fax,email,website">
<cfloop list="#fieldList#" index="i">      
#myQuery[i]#
</cfloop>

这项工作不会给我一个错误吗?我觉得我只是在忽略一些简单的事情,只是在任何地方都找不到答案。

Shouldn't this work without giving me an error? I feel like I'm just overlooking something simple I just can't find the answer anywhere.

推荐答案

好的,我看到了修改了您的初始代码,所以这是我的回答:

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>

这将为您提供所需的输出。具有query属性的外部cfoutput将遍历您收到的所有数据行。然后,对于每一行,您遍历查询产生的列的列表,并针对每一列,输出数据,通过myQuery.CurrentRow指定行,该行将通过外部输出自动为您进行迭代。

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.

这篇关于尝试输出查询值时遇到复杂的对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆