Connecting Tech Pros Worldwide Forums | Help | Site Map

list fieldnames in table? (from PHP)

Miles Keaton
Guest
 
Posts: n/a
#1: Nov 23 '05
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@postgresql.org so that your
message can get through to the mailing list cleanly

Steven Klassen
Guest
 
Posts: n/a
#2: Nov 23 '05

re: list fieldnames in table? (from PHP)


* Miles Keaton <mileskeaton@gmail.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.nspname = 'public'
AND relnamespace = pg_namespace.oid
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

Michael Fuhr
Guest
 
Posts: n/a
#3: Nov 23 '05

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:

http://www.postgresql.org/docs/7.4/static/catalogs.html

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

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

Justin Wyer
Guest
 
Posts: n/a
#4: Nov 23 '05

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@postgresql.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

Scott Marlowe
Guest
 
Posts: n/a
#5: Nov 23 '05

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_schema to examine


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

Joshua D. Drake
Guest
 
Posts: n/a
#6: Nov 23 '05

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:

http://us2.php.net/manual/en/print/f...field-name.php

Sincerely,

Joshua D. Drake


[color=blue]
>
>In addition to the other ideas given here, you also have the SQL spec
>standard information_schema to examine
>
>
>---------------------------(end of broadcast)---------------------------
>TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.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@commandprompt.com - http://www.commandprompt.com
PostgreSQL Replicator -- production quality replication for PostgreSQL


GreyGeek
Guest
 
Posts: n/a
#7: Nov 23 '05

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@postgresql.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("select * 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
Closed Thread