?
Solved

How to get a list of columns for the current query

Posted on 2000-02-17
14
Medium Priority
?
129 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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
 
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 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