Solved

How to get a list of columns for the current query

Posted on 2000-02-17
14
126 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 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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Suggested Courses

737 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