list fieldnames in table? (from PHP)

Started by Miles Keatonover 21 years ago7 messagesgeneral
Jump to latest
#1Miles Keaton
mileskeaton@gmail.com

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?

#2Steven Klassen
sklassen@commandprompt.com
In reply to: Miles Keaton (#1)
Re: list fieldnames in table? (from PHP)

* Miles Keaton <mileskeaton@gmail.com> [2004-10-25 19:36:43 -0700]:

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

#3Michael Fuhr
mike@fuhr.org
In reply to: Miles Keaton (#1)
Re: list fieldnames in table? (from PHP)

On Mon, Oct 25, 2004 at 07:36:43PM -0700, 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

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/

#4Justin Wyer
justin@isogo.co.za
In reply to: Miles Keaton (#1)
Re: list fieldnames in table? (from PHP)

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?

---------------------------(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

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

#5Scott Marlowe
smarlowe@qwest.net
In reply to: Miles Keaton (#1)
Re: list fieldnames in table? (from PHP)

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?

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

#6Joshua D. Drake
jd@commandprompt.com
In reply to: Scott Marlowe (#5)
Re: list fieldnames in table? (from PHP)

Scott Marlowe wrote:

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/function.pg-field-name.php

Sincerely,

Joshua D. Drake

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

-- 
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
#7GreyGeek
jkreps@neb.rr.com
In reply to: Miles Keaton (#1)
Re: list fieldnames in table? (from PHP)

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?

---------------------------(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

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