Solved

postgres and php

Posted on 2002-05-27
6
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 40

Accepted Solution

by:
Richard Quadling 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:Richard Quadling
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:Richard Quadling
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7038834
Ah! Now it shows my messages!!!!

0
 
LVL 40

Expert Comment

by:Richard Quadling
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 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