n1875621
asked on
postgres and php
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
<?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.
Ah! Now it shows my messages!!!!
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.
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.
ASKER
worked like a charm.. thanks.
But if you can work THAT out, then you've pretty much got the whole thing!
Regards,
Richard Quadling.