By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,541 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,541 IT Pros & Developers. It's quick & easy.

list fieldnames in table? (from PHP)

P: n/a
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 ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* Miles Keaton <mi*********@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

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

Nov 23 '05 #2

P: n/a
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/

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

Nov 23 '05 #3

P: n/a
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 ma*******@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...

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

Nov 23 '05 #4

P: n/a
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
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #5

P: n/a
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/f...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 ma*******@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
Nov 23 '05 #6

P: n/a
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 ma*******@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
Nov 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.