list fieldnames in table? (from PHP)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Miles Keaton

    list fieldnames in table? (from PHP)

    Is there a simple way to list fieldnames in a table, from PHP?

    When on the command-line, I just do \d tablename

    But how to get the fieldnames from PHP commands?

    ---------------------------(end of broadcast)---------------------------
    TIP 3: if posting/reading through Usenet, please send an appropriate
    subscribe-nomail command to majordomo@postg resql.org so that your
    message can get through to the mailing list cleanly

  • Steven Klassen

    #2
    Re: list fieldnames in table? (from PHP)

    * Miles Keaton <mileskeaton@gm ail.com> [2004-10-25 19:36:43 -0700]:
    [color=blue]
    > Is there a simple way to list fieldnames in a table, from PHP?
    >
    > When on the command-line, I just do \d tablename
    >
    > But how to get the fieldnames from PHP commands?[/color]

    If your namespace is 'public' and your table is 'users', for example:

    SELECT attname
    FROM pg_namespace, pg_attribute, pg_type, pg_class
    WHERE pg_type.oid = atttypid
    AND pg_class.oid = attrelid
    AND pg_namespace.ns pname = 'public'
    AND relnamespace = pg_namespace.oi d
    AND relname = 'users'
    AND attnum >= 1;

    --
    Steven Klassen - Lead Programmer
    Command Prompt, Inc. - http://www.commandprompt.com/
    PostgreSQL Replication & Support Services, (503) 667-4564

    ---------------------------(end of broadcast)---------------------------
    TIP 7: don't forget to increase your free space map settings

    Comment

    • Michael Fuhr

      #3
      Re: list fieldnames in table? (from PHP)

      On Mon, Oct 25, 2004 at 07:36:43PM -0700, Miles Keaton wrote:[color=blue]
      > Is there a simple way to list fieldnames in a table, from PHP?
      >
      > When on the command-line, I just do \d tablename[/color]

      If you run "psql -E" or type "\set ECHO_HIDDEN" after you're
      in psql then you'll see the hidden queries that psql sends for
      "\d tablename", etc. Examine those queries and use the relevant
      parts in your own code.

      You might want to familiarize yourself with the system catalogs,
      which is what you'll be querying:



      --
      Michael Fuhr


      ---------------------------(end of broadcast)---------------------------
      TIP 8: explain analyze is your friend

      Comment

      • Justin Wyer

        #4
        Re: list fieldnames in table? (from PHP)

        Miles Keaton wrote:
        [color=blue]
        >Is there a simple way to list fieldnames in a table, from PHP?
        >
        >When on the command-line, I just do \d tablename
        >
        >But how to get the fieldnames from PHP commands?
        >
        >---------------------------(end of broadcast)---------------------------
        >TIP 3: if posting/reading through Usenet, please send an appropriate
        > subscribe-nomail command to majordomo@postg resql.org so that your
        > message can get through to the mailing list cleanly
        >
        >[/color]
        If you have a look at the PHP manual there is a function to do this for
        you - pg_meta_data - check out the manual...

        ---------------------------(end of broadcast)---------------------------
        TIP 7: don't forget to increase your free space map settings

        Comment

        • Scott Marlowe

          #5
          Re: list fieldnames in table? (from PHP)

          On Mon, 2004-10-25 at 20:36, Miles Keaton wrote:[color=blue]
          > Is there a simple way to list fieldnames in a table, from PHP?
          >
          > When on the command-line, I just do \d tablename
          >
          > But how to get the fieldnames from PHP commands?[/color]

          In addition to the other ideas given here, you also have the SQL spec
          standard information_sch ema to examine


          ---------------------------(end of broadcast)---------------------------
          TIP 3: if posting/reading through Usenet, please send an appropriate
          subscribe-nomail command to majordomo@postg resql.org so that your
          message can get through to the mailing list cleanly

          Comment

          • Joshua D. Drake

            #6
            Re: list fieldnames in table? (from PHP)

            Scott Marlowe wrote:
            [color=blue]
            >On Mon, 2004-10-25 at 20:36, Miles Keaton wrote:
            >
            >[color=green]
            >>Is there a simple way to list fieldnames in a table, from PHP?
            >>
            >>When on the command-line, I just do \d tablename
            >>
            >>But how to get the fieldnames from PHP commands?
            >>
            >>[/color][/color]
            Hello,

            This PHP function will give you what you need:



            Sincerely,

            Joshua D. Drake


            [color=blue]
            >
            >In addition to the other ideas given here, you also have the SQL spec
            >standard information_sch ema to examine
            >
            >
            >---------------------------(end of broadcast)---------------------------
            >TIP 3: if posting/reading through Usenet, please send an appropriate
            > subscribe-nomail command to majordomo@postg resql.org so that your
            > message can get through to the mailing list cleanly
            >
            >[/color]


            --
            Command Prompt, Inc., home of Mammoth PostgreSQL - S/ODBC and S/JDBC
            Postgresql support, programming shared hosting and dedicated hosting.
            +1-503-667-4564 - jd@commandpromp t.com - http://www.commandprompt.com
            PostgreSQL Replicator -- production quality replication for PostgreSQL


            Comment

            • GreyGeek

              #7
              Re: list fieldnames in table? (from PHP)

              Miles Keaton wrote:
              [color=blue]
              > Is there a simple way to list fieldnames in a table, from PHP?
              >
              > When on the command-line, I just do \d tablename
              >
              > But how to get the fieldnames from PHP commands?
              >
              > ---------------------------(end of broadcast)---------------------------
              > TIP 3: if posting/reading through Usenet, please send an appropriate
              > subscribe-nomail command to majordomo@postg resql.org so that your
              > message can get through to the mailing list cleanly[/color]

              Here is one way Python can do it through ODBC:
              # fetch descriptions to create field name dictionaries
              try:
              ci = db.cursor()
              ci.execute("sel ect * from PERSINFO where 1 = 0")
              column = 0
              for d in ci.description: # key : value
              PersFields[d[0]] = column # field name : position
              PersPos[column] = d[0] # position : field name d[0]
              PersTypes[d[0]] = d[1] # field name : data type d[1]
              PersPrec[d[0]] = d[4] # field name : precision d[4]
              PersScale[d[0]] = d[5] # field name : scale d[5]
              PersVals[column] = None # position : value (init=None)
              column += 1
              ci.close()
              ci = None

              --
              --
              GreyGeek

              Comment

              Working...