Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

postgres and php

Posted on 2002-05-27
6
Medium Priority
?
413 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 800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to implement server side field validation and display customized error messages to the client.
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 count occurrences of each item in an array.

719 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