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

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

问题描述

我的目标是只输出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>

这将为您提供所需的输出.带有查询属性的外部 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天全站免登陆