Solved

postgres and php

Posted on 2002-05-27
6
403 Views
Last Modified: 2008-03-06
i want to be able to obtain a list of columns in a database table in PHP...

for example

a table in postgres that looked like...

create table mytable
(
 id serial,
 age int,
 name varchar,
)

i would want to get an array with 'id', 'age' and 'name' in them.

there are no defined functions in php to do this with postgres. the pg_metadata function is no good, i am not running a compatiable version of php and also the function is in experiemental phase.

Thanks for any help you can give me.

cheers
john
0
Comment
Question by:n1875621
  • 5
6 Comments
 
LVL 40

Accepted Solution

by:
RQuadling earned 200 total points
ID: 7038813
Something you COULD try is ...

<?php

$db = pg_connect(blah blah blah);
$res = pg_query($db,"SELECT * FROM mytable LIMIT 1,1");
$fieldnames = "";
for ($i = 0 ; $i < pg_num_fields($res) ; $i++)
 {
 $fieldnames[$i] = pg_field_name($res,$i);
 }
?>

Ok, this is ultra basic, but should be OK!

Regards,

Richard Quadling.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7038819
If there is no data in the table, then I do not know how to issue a query that will return a null for each column.

But if you can work THAT out, then you've pretty much got the whole thing!

Regards,

Richard Quadling.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7038833
Something you COULD try is ...

<?php

$db = pg_connect(blah blah blah);
$res = pg_query($db,"SELECT * FROM mytable LIMIT 1,1");
$fieldnames = "";
for ($i = 0 ; $i < pg_num_fields($res) ; $i++)
{
$fieldnames[$i] = pg_field_name($res,$i);
}
?>

Ok, this is ultra basic, but should be OK!

If there is no data in the table, then I do not know how to issue a query that will return a null for
each column.

But if you can work THAT out, then you've pretty much got the whole thing!

Regards,

Richard Quadling.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:RQuadling
ID: 7038834
Ah! Now it shows my messages!!!!

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 7038836
And I've just realised, if your result set is empty, then the columns are still present, but empty.

So, the above query should work even without any data!

I am not sure on the LIMIT syntax, but basically I am saying start with the first record and only get 1 record.

Regards,

Richard Quadling.
0
 

Author Comment

by:n1875621
ID: 7040726
worked like a charm.. thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
uploading image to a profile page 7 36
Integrate Office 365 calendar events into WordPress sites 2 53
<? versus <?php 5 37
Problem sending file attachments 8 23
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now