if I do a slq query like this

"SELECT id AS foo, FROM mytable"

is it possible to get the actual column name (not the alias)

I've tried mysql_fetch_field, but it says the column name is 'foo' the actual column name is 'id'.

    Hi Gasolene

    I don't understand your question - the column name is 'id'. What is it you want to know?

    Norm

      It says the name is foo because that is the alias you have told it to use. If you want the resultset to use the original column name then loose the alias.

      "SELECT id FROM mytable"

        to clarify, I realize in that query, the column name is 'id'.

        That sql statement was just an example, the actual sql statement could be anything (unknown to me). This is inside a reusable object thus It needs to work with any sql statement.

        The select statement may or may not use aliases. (again, unknown to me)

        I need a way of getting the column name even when aliases used. this is an easy task using ado recordset object, i was just wondering if it's possible to do with a trick in php.

        Perhaps there is some way to determine the column index in the table, then I could use another query to get actual name, or something like that...

          Hi Gasolene

          You can do this:

          $var = mysql_query("DESCRIBE table");
          echo $var;

          This will probably give you some horrible mess which will need formatting, but you get the idea. As it turns out, you can also do this:

          $var = mysql_query("SHOW COLUMNS FROM table");
          echo $var;

          It's all explained in fascinating detail in the MySQL manual.

          HTH

          Norm

            You can use Explain, Describe, and Show Columns to find out table structure, which you use depends on your needs and circumstances. Search the manual for info about each.

              I'm not sure that an sql statement is what I'm looking for.

              remember, I have no control over the sql statement or column order.

              all I have is the table name and array of column aliases. I do not have their column number.

              I can get table structure just fine using "DESCRIBE TABLE" but that doesn't tell me what columns in my query are the columns in the table.

              From what I've read, I don't think this is possible in php. Unless someone else can shine a light n this??????

                Write a Reply...