Solved

How to get a list of columns for the current query

Posted on 2000-02-17
14
123 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
ID: 2531140
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
ID: 2531694

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
ID: 2531734
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
ID: 2531849
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
ID: 2531884
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
ID: 2531928
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
ID: 2531994
<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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Expert Comment

by:nathans
ID: 2532010
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
ID: 2532051
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
ID: 2532249
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
ID: 2532281
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
ID: 2532350
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
ID: 2532775
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
ID: 2532845
Thanks,
Nathan Stanford
Mr. ColdFusion
-------------------------------------
CFTips+  FREE e-ZINE on ColdFusion
subscribe-cftips@nsnd.com
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

19 Experts available now in Live!

Get 1:1 Help Now