dbTalk Databases Forums  

list fieldnames in table? (from PHP)

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss list fieldnames in table? (from PHP) in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Miles Keaton
 
Posts: n/a

Default list fieldnames in table? (from PHP) - 10-25-2004 , 09:36 PM






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 (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


Reply With Quote
  #2  
Old   
Steven Klassen
 
Posts: n/a

Default Re: list fieldnames in table? (from PHP) - 10-25-2004 , 10:53 PM






* Miles Keaton <mileskeaton (AT) gmail (DOT) com> [2004-10-25 19:36:43 -0700]:

Quote:
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?
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



Reply With Quote
  #3  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: list fieldnames in table? (from PHP) - 10-25-2004 , 11:04 PM



On Mon, Oct 25, 2004 at 07:36:43PM -0700, Miles Keaton wrote:
Quote:
Is there a simple way to list fieldnames in a table, from PHP?

When on the command-line, I just do \d tablename
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



Reply With Quote
  #4  
Old   
Justin Wyer
 
Posts: n/a

Default Re: list fieldnames in table? (from PHP) - 10-26-2004 , 01:29 AM



Miles Keaton wrote:

Quote:
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 (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly


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



Reply With Quote
  #5  
Old   
Scott Marlowe
 
Posts: n/a

Default Re: list fieldnames in table? (from PHP) - 10-26-2004 , 08:24 PM



On Mon, 2004-10-25 at 20:36, Miles Keaton wrote:
Quote:
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?
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 (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #6  
Old   
Joshua D. Drake
 
Posts: n/a

Default Re: list fieldnames in table? (from PHP) - 10-26-2004 , 09:52 PM



Scott Marlowe wrote:

Quote:
On Mon, 2004-10-25 at 20:36, Miles Keaton wrote:


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?


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



Quote:
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 (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



--
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 (AT) commandprompt (DOT) com - http://www.commandprompt.com
PostgreSQL Replicator -- production quality replication for PostgreSQL




Reply With Quote
  #7  
Old   
GreyGeek
 
Posts: n/a

Default Re: list fieldnames in table? (from PHP) - 10-26-2004 , 11:02 PM



Miles Keaton wrote:

Quote:
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 (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly
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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.