Thread: Column names

Column names

From
"Kelvin Varst"
Date:
Hi,

When I make a simple "SELECT * FROM table;" query, is there somewhere in
that query I can find the names of the columns?

Kelvin :-)



Re: Column names

From
Keary Suska
Date:
on 11/3/02 2:26 AM, kelvin@varst.dk purportedly said:

> When I make a simple "SELECT * FROM table;" query, is there somewhere in
> that query I can find the names of the columns?

What exactly do you need to do? The column names are available to PHP. You
can determine them dynamically by using pg_fetch_assoc() and applying
array_keys() to the resulting aray.

Keary Suska
Esoteritech, Inc.
"Leveraging Open Source for a better Internet"


Re: Column names

From
Andrew McMillan
Date:
On Sun, 2002-11-03 at 22:26, Kelvin Varst wrote:
> Hi,
>
> When I make a simple "SELECT * FROM table;" query, is there somewhere in
> that query I can find the names of the columns?

In PHP if you get the rows with:

$row = pg_Fetch_Object( $result, $rownum);

You will be able to refer to them by name as $row->fieldname
$row->otherfield and so on.

This is very useful :-)


Otherwise, if you want to know the column names, something like:

SELECT attname FROM pg_attribute WHERE attrelid = (SELECT oid FROM
pg_class WHERE relname = 'mytable') AND attnum > 0 ORDER BY attnum;

will get them for you.

Regards,
                    Andrew.
--
---------------------------------------------------------------------
Andrew @ Catalyst .Net.NZ Ltd, PO Box 11-053, Manners St,  Wellington
WEB: http://catalyst.net.nz/         PHYS: Level 2, 150-154 Willis St
DDI: +64(4)916-7201     MOB: +64(21)635-694    OFFICE: +64(4)499-2267
           Survey for nothing with http://survey.net.nz/
---------------------------------------------------------------------


Re: Column names

From
"Rommel B. Abaya"
Date:
$result = pg_exec($conn,"select * from table");
if (pg_numrows($result)) {
      for($i=0;$i<pg_numfields($result);$i++) {
      print "<td>".pg_fieldname($result,$i)."</td>\n";
   }

}

HTH

Andrew McMillan wrote:

> On Sun, 2002-11-03 at 22:26, Kelvin Varst wrote:
> > Hi,
> >
> > When I make a simple "SELECT * FROM table;" query, is there somewhere in
> > that query I can find the names of the columns?
>
> In PHP if you get the rows with:
>
> $row = pg_Fetch_Object( $result, $rownum);
>
> You will be able to refer to them by name as $row->fieldname
> $row->otherfield and so on.
>
> This is very useful :-)
>
> Otherwise, if you want to know the column names, something like:
>
> SELECT attname FROM pg_attribute WHERE attrelid = (SELECT oid FROM
> pg_class WHERE relname = 'mytable') AND attnum > 0 ORDER BY attnum;
>
> will get them for you.
>
> Regards,
>                                         Andrew.
> --
> ---------------------------------------------------------------------
> Andrew @ Catalyst .Net.NZ Ltd, PO Box 11-053, Manners St,  Wellington
> WEB: http://catalyst.net.nz/         PHYS: Level 2, 150-154 Willis St
> DDI: +64(4)916-7201     MOB: +64(21)635-694    OFFICE: +64(4)499-2267
>            Survey for nothing with http://survey.net.nz/
> ---------------------------------------------------------------------
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly