954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Query to Variable Array

Hello I'm new to ColdFusion, for my work I've been asked to learn it..

I'm coming from an extensive PHP background. For the life of me I cannot figure out how to simply take a query from SQL and load it into an array and then display it. I though this would be a very simple task which has become a headache.

Anyhow come code:

<!--- Show user details --->
<cfquery name="at" datasource="sql">
SELECT * FROM tbladmins
</cfquery>

<cfset FieldIArray = ListToArray(at)>


<cfoutput>#FieldIArray[0]#</cfoutput>


The error lies in the cfset field, I don't understand how I can extract data from the query?

Can someone help thanks..

Abakiz
Newbie Poster
6 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

cfquery returns information as a structure so if you want to exact data you can use queryname.columnname so in this case for example #at.username# would return whatever the first row of the query is.

You can use cfdump to view what the query is returning by doing cfdump(#at#).

Line 6 errors because cfquery does not return a list, it is a structure.

If you want to loop the query:

<cfloop query="at">
   <cfoutput>#at.lastName#, #at.firstName# </cfoutput>
</cfloop>


OR

<cfoutput query="at">
   #at.lastName#, #at.firstName#
</cfoutput>


lastName and firstName would be my guess of column names since you used *

Let me know if this helps, you really dont need to convert this to an array unless there is more behind the question.

cfwebdeveloper
Junior Poster in Training
78 posts since May 2011
Reputation Points: 19
Solved Threads: 8
 

I'll get back to you on Sunday night (at a lan event). Thanks for the help!

Abakiz
Newbie Poster
6 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

Thanks for the help, it works great! :)

Abakiz
Newbie Poster
6 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

No problem, just mark this thread as solved. :D

cfwebdeveloper
Junior Poster in Training
78 posts since May 2011
Reputation Points: 19
Solved Threads: 8
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You