Results 1 to 7 of 7

Thread: How can I return fieldname?

  1. #1
    Join Date
    Apr 2005
    Posts
    6

    How can I return fieldname?

    I'm using MySQL. I'd like a query along the lines of:

    SELECT * from table_name

    that returns the following:

    field1_name, field1_value, field2_name, field2_value, ...

    How can I do this?

  2. #2
    Join Date
    Feb 2003
    Posts
    1,048
    You can't do that with a simple query. You have to come up with a process to build this.

  3. #3
    Join Date
    Apr 2005
    Posts
    6
    How do you mean?

  4. #4
    Join Date
    Feb 2003
    Posts
    1,048
    I mean that you can't dimply return a field name as a value in the select query. You have to come up with a process that builds the data set int he manner you want.

    I've done it with SQL Server, and there isn't a simple way to do it.

  5. #5
    Join Date
    Apr 2005
    Posts
    6
    What direction should I look into doing this?

  6. #6
    Join Date
    Jul 2005
    Location
    College Station, TX
    Posts
    2
    you could use the following

    $sql = "SELECT * FROM table";
    $query = mysql_query($sql,$connection);
    $columns = mysql_num_fields($query);

    iterate through the columns

    for($i = 0; $i < $columns; $i++) {
    print mysql_field_name($columns,$i);
    }


    or the query below to return column names for the table, in conjuction with your other query and some code to do the rest.

    show columns from [table name];

  7. #7
    Join Date
    Feb 2003
    Posts
    1,048
    Yeah, it's much easier to get the results you want via client-side code if you are using the query in that manner.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •