Solved

postgres and php

Posted on 2002-05-27
6
412 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
Technology Partners: 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
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…

624 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