13, May 2008

query - first result is all column names - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  Databases
Topic: query - first result is all column names
« previous next »
Pages: [1] Print

Author Topic: query - first result is all column names  (Read 311 times)
Community Supporter ?
OMG!I am geek
**
Gender: Male
Posts: 56
364 credits
Members referred : 0


Schwa?


« on: Feb 09, 2008, 05:15:44 AM »

Lets say you have a 3 column database with colums of 'id', 'username' and 'registrationDate'

So lets say you want to generate a csv with somethignl ike mysql, you can.... but lets put the column names on the top.  Cool


Select 'id', 'username', 'registrationDate'
union
Select * from tableName;

coolios. 


Now here's the thing - how would you make it be a dynamic thing?    I don't want to modify this particular query whenever I add a new column.


(If you ask why - go for it - I'll explain - but I don't think its needed for the solution.)

Any ideas are more than welcome.
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6236
38242 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Feb 09, 2008, 09:16:28 AM »

is ity this what you mean:

$result = mysql_query(sprintf("SELECT * FROM %s", $this->table));
      $i = 0;
      while ($i < mysql_num_fields($result)) {
         $type  = mysql_field_type($result, $i);
            $name  = mysql_field_name($result, $i);
         $this->table_fields[$name] = $type;
         $i++;
      }


Last blog : Database Management with phpMyAdmin
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7824
39879 credits
Members referred : 3



« Reply #2 on: Feb 09, 2008, 12:03:29 PM »

You can do what you want with 2 queries. The one is this :

SHOW COLUMNS FROM `table`

Then get the `Field` field from that table, and then call a

SELECT * FROM `table`

to fetch all the data.

Trial and Error my two best teachers Cool
Promote your blog for free.... Visit through proxy

Last blog : Keep it Legal - Tims guide to legal notices
Community Supporter ?
OMG!I am geek
**
Gender: Male
Posts: 56
364 credits
Members referred : 0


Schwa?


« Reply #3 on: Feb 10, 2008, 04:30:12 AM »

Let me clear it up - it all has to be written as a SQL script or a set of them that can be ran into the mysql CLI.

So - olaf: no access to PHP
Nikolas: show columns shows the columns with descriptive data in rows.  I need the results in one row - multiple columns.

Thanks!
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7581
Tags : dynamic headers Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  Databases
Topic: query - first result is all column names
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 13, 2008, 07:35:40 PM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 34.931
Total Topics: 7.262
Total Members: 3.482
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: ondho

15 Guests, 4 Users online :

12 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.