473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

Nov 23 '05 #1
6 4683
* 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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sam | last post by:
I have a function writen in Perl that takes a name-value list (array) as argument and returns a name-value list (array). My question is: How to call this function from PHP and get the returned...
4
by: steve | last post by:
I have an on-going problem that my ".MYI" mysql files get corrupted. I have written php trapping routine to report on the problem to admin. My next step is to initiate "repair table" right after...
11
by: alex | last post by:
Hi, I am looking for a way to populate an HTML table from an external local text file which looks like this: DATE/TIME LAT. LON. DEPTH. ML....
3
by: mh | last post by:
Hello, I'm trying to create a table from a PHP script. >From what I read in my book about PHP and MySQL I should do something like that: $sql_query="CREATE TABLE '$num' ( 'variable1' ...
5
by: nescio | last post by:
hello, i have made an application in php so that people can make, on the fly, a form. when they submit the form there is a javascript formvalidation. because we do not know how many fields...
9
by: Lorenzo Thurman | last post by:
I'm connecting to an access database and I would like the query to return results grouped on a particular column. When I try the following query, I get this error back from PHP: DB Error: syntax...
0
by: jamie | last post by:
I am trying to populate a mysql table from the contents of a <SELECT> list using PHP. The list is populated from another list and from there all the items need to be saved to a table.
2
by: abbeyro | last post by:
Hello, I'm trying to retrieve a list of tables from all_tables in a given Oracle db. I'm using System.Data.OracleClient, OracleCommand and OracleDataReader and I always obtain the results in...
13
by: trpost | last post by:
I am looking for a way to send data from one page to another as POST data without using forms or cURL. I have a php script that is passing a list of cases from on page to another when a link is...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.