Solved

postgres and php

Posted on 2002-05-27
6
400 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
Ah! Now it shows my messages!!!!

0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
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
Comment Utility
worked like a charm.. thanks.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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 count occurrences of each item in an array.

744 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

18 Experts available now in Live!

Get 1:1 Help Now