Solved

How to get a list of columns for the current query

Posted on 2000-02-17
14
122 Views
Last Modified: 2013-12-24
Simple question - How can I access a list of columns for a query, and how can I then get data from the columns. Eg, if I have a table containing columns Name, Address, Phone, how do I get a list of the three column names, and how do I then access the data in the columns - I cant go MyQuery.Phone because I dont know the names of the columns that will be present, and how many there will be.
0
Comment
Question by:Arachn1d
  • 6
  • 6
  • 2
14 Comments
 
LVL 1

Expert Comment

by:bigbadb
Comment Utility
query_name.ColumnList

Returns a comma-delimited list of the query columns

Then you can read it into an array then access it that way

Hope this helps
0
 
LVL 5

Accepted Solution

by:
nathans earned 50 total points
Comment Utility

I tested this and it works.
Nathan

<cfset datasource ="yourdatasource">
<cfset table ="yourtable">
<!--- Simply change the above datasource and table --->


<CFQUERY NAME="getcolumns" DATASOURCE="#datasource#">
SELECT      *
FROM         #table#
</CFQUERY>

<CFSET MyArray = ArrayNew(1)>


<html>
<head>
      <title></title>
</head>

<body>

<!--- <CFSET Item = ListGetAt(columnlist, lo, ",")>
 --->
<cfoutput>
<cfset cnt = 1>
<cfloop index="loopid" list="#getcolumns.columnlist#">
<cfset MyArray[#cnt#] = "#loopid#">
<cfset cnt = cnt + 1>
</cfloop>
<cfset length = cnt-1>
<table border="1" bordercolor="000099" cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellpadding="0" cellspacing="0" width="90%">
<tr>
<cfloop index="tablecol" from="1" to="#length#">
<td bgcolor="000099"><font face="Arial" color="ffffff" size="-2"><b>#MyArray[tablecol]#&nbsp;&nbsp;&nbsp;</b> </font></td>
</cfloop>
</tr>
<cfloop index="tablerow" from="1" to="#getcolumns.recordcount#">
<tr bgcolor="#IIf(tablerow Mod 2, DE('ffffff'), DE('EEEEEE'))#">
<cfloop index="tablecol" from="1" to="#length#">
<td><font face="Arial" color="000099" size="-2">
<cfset test = "getcolumns.#MyArray[tablecol]#">

#Evaluate(test)#
</font></td>
</cfloop>
</tr>
</cfloop>
</cfoutput>

</tr></table>
</td></tr></table>
</body>
</html>
0
 
LVL 1

Expert Comment

by:bigbadb
Comment Utility
nice try stealing my answer nathans.  Query.columnlist works i figured you could come up with the array on your own
Arachn1d

good luck
0
 
LVL 5

Expert Comment

by:nathans
Comment Utility
It takes MORE then just an array... you have to dynamically loop thru it as well.

==================================
More refined code.
==================================


<cfset datasource ="nsnd">
<cfset table ="cfMessageBoard">
<!---
<cfset datasource ="#url.ds#">
<cfset table ="#url.table#">
 --->


<CFQUERY NAME="getcolumns" DATASOURCE="#datasource#">
SELECT      *
FROM         #table#
</CFQUERY>

<CFSET MyArray = ArrayNew(1)>


<html>
<head>
      <title></title>
</head>

<body>

<!--- <CFSET Item = ListGetAt(columnlist, lo, ",")>
 --->
<cfoutput>
<cfset cnt = 1>
<cfloop index="loopid" list="#getcolumns.columnlist#">
<cfset MyArray[#cnt#] = "#loopid#">
<cfset cnt = cnt + 1>
</cfloop>
<cfset length = cnt-1>
<table border="1" bordercolor="000099" cellpadding="0" cellspacing="0"><tr><td>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<cfloop index="tablecol" from="1" to="#length#">
<td bgcolor="000099"><font face="Arial" color="ffffff" size="-2"><b>#MyArray[tablecol]#&nbsp;&nbsp;&nbsp;</b> </font></td>
</cfloop>
</tr>
</cfoutput>
<cfoutput query="getcolumns">
<tr bgcolor="#IIf(CurrentRow Mod 2, DE('ffffff'), DE('EEEEEE'))#">
<cfloop index="tablecol" from="1" to="#length#">
<cfset test = "getcolumns.#MyArray[tablecol]#">
<td align="center"><font face="Arial" color="000099" size="-2">
 #Evaluate(test)#
</font></td>
</cfloop>
</tr>
</cfoutput>
</table>
</td></tr></table>


</body>
</html>
0
 
LVL 1

Expert Comment

by:bigbadb
Comment Utility
no its not you can parse that comma delem list into an array.  Have a counter var.  Then he will know how many columns he is dealing with and insert accordingly
0
 
LVL 5

Expert Comment

by:nathans
Comment Utility
I am talking about to use it.  Maybe we are saying the same thing what is your example?
0
 
LVL 1

Expert Comment

by:bigbadb
Comment Utility
<cfquery datasource="ohiotest" name="testme">
      select *
      from users
</cfquery>
<CFOUTPUT>
#TESTME.ColumnList#
<cfset holdtest = listtoarray(#testme.columnlist#)>
#arraylen(holdtest)#
</CFOUTPUT>

This right here returns the number of columns and you would access it just like an array from 1 to the arraylen()
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:nathans
Comment Utility
If I understand correctly he wants to get the data in each of the columns as well as the column names which is what I have done.

>
> how do I then access the data in the
> columns - I cant go MyQuery.Phone
> because I dont know the names of the
> columns that will be present, and how
> many there will be.
>

0
 
LVL 1

Expert Comment

by:bigbadb
Comment Utility
you just loop through

loop through the recordcount
then with in the recordcount loop you have another loop to read each rows column from 1 to the array len


0
 

Author Comment

by:Arachn1d
Comment Utility
Thankyou all for the great help, but AFAIK, none of you have answered the other part of my question - once I have the column list, how do I access the data in it? So I have an array/list/whatever of the columns. How do I access the data in the first column? The third one? Etc.
0
 
LVL 1

Expert Comment

by:bigbadb
Comment Utility
this is done in the second loop

ie


<cfloop from="1" to="query.recordcount" index="count">

  <cfloop from="1" to="arraylen" index="count2">
     <cfset hold = "query.#array[count2]#">
     #Evaluate(hold)#
  </cfloop>
</cfloop>


THis would spit out each col and data within
0
 
LVL 5

Expert Comment

by:nathans
Comment Utility
My second post of code you can cut and past as an example.  All you have to modify to see how it works is change the to cfset statements at the top of the code and you will see that this code will work.

I created the code for myself when I saw your post so I know it works. test it out.  

You will get the columns in a table with the data in the columns.
0
 

Author Comment

by:Arachn1d
Comment Utility
Thanks for the answer. I found nathans was the first to actually explain how to access a column when you don't know it's name. If anyone else feels they deserve points, let me know, although I am a bit short (points, not feet high). One final question - how can I get the datatype for a column?
0
 
LVL 5

Expert Comment

by:nathans
Comment Utility
Thanks,
Nathan Stanford
Mr. ColdFusion
-------------------------------------
CFTips+  FREE e-ZINE on ColdFusion
subscribe-cftips@nsnd.com
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now