How to get fields list of ANY query that returns no records

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
martinwronski
Forum Newbie
Posts: 4
Joined: Mon Jul 31, 2006 2:15 pm

How to get fields list of ANY query that returns no records

Post by martinwronski »

Hi
Is it possible? If it is how to do it?
Example:
Query = SELECT em_login, em_name FROM employee
And table employee has no records.
So how to get column names: em_login, em_name?

Thx 4 ur help
Martin
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post by kbrown3074 »

describe employee
martinwronski
Forum Newbie
Posts: 4
Joined: Mon Jul 31, 2006 2:15 pm

Post by martinwronski »

thx, I know that
But I want to read that info from ANY query with multiple joins, subqueries, group by’s etc
is it possible?
psql returns column names event if there is no data returned but how to do this in php?

or how about this
SELECT em_login, em_salary * 12 AS year_salary FROM employee
how to find out that this query returns em_login/year_salary fields?
martinwronski
Forum Newbie
Posts: 4
Joined: Mon Jul 31, 2006 2:15 pm

Post by martinwronski »

I have just found one way to do it by calling psql as an external program:
e.g.
exec("psql -h host -d database -U user -o \"$sFile\" -c \"$sQuery\"");

I have to generete psql results to file coz if I run psql with no -o switch it throws an exceptoion (under win2k).
also i have to remove all EOL to make it works fine:

$sQuery = str_replace("\n", ' ', $sQuery);

if anyone know the better way to do it please post it.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oops.
ok, it's pg_field_num and pg_field_name then ;)
martinwronski
Forum Newbie
Posts: 4
Joined: Mon Jul 31, 2006 2:15 pm

Post by martinwronski »

Works perfectly, thanks!
I got no bloody idea how i could miss this in pg/php documentation ;)
Post Reply