[postgis-users] Identifying field names in select out of order

Sean M. Montague smontague at ATSDenver.com
Tue Feb 21 09:51:25 PST 2006


Thanks Mark,

I somewhat got it to work.  I used:

    $sql2 = "SELECT 'lastname','effective_date'
                From tblateammembers";

    $res = pg_query($conn, $sql2);
    
    for ($i = 0; $i <  pg_numrows($res); $i++) {

      	$row = pg_fetch_array($res, $i, PGSQL_NUM);

      	foreach ($row as $field => $value ){

      		print "fieldname: " . $field . "\n";
      		print "value    : " . $value . "\n";
      	}
    }

$field gives the field indices of the field in the SELECT statement,
e.g. lastname=0, and $value gives the field name in this case.  I wasn't
actually able to extract the value of the row from the field.  Thanks.

Sean

-----Original Message-----
From: postgis-users-bounces at postgis.refractions.net
[mailto:postgis-users-bounces at postgis.refractions.net] On Behalf Of Mark
Cave-Ayland
Sent: Tuesday, February 21, 2006 9:59 AM
To: 'PostGIS Users Discussion'
Subject: RE: [postgis-users] Identifying field names in select out of
order

 

> -----Original Message-----
> From: postgis-users-bounces at postgis.refractions.net 
> [mailto:postgis-users-bounces at postgis.refractions.net] On 
> Behalf Of Sean M. Montague
> Sent: 21 February 2006 16:46
> To: PostGIS Users Discussion
> Subject: RE: [postgis-users] Identifying field names in 
> select out of order
> 
> Thanks Don,
> 
> I am using php. Problem with pg_fetch_assoc() is that I need 
> to know the field name.  I want to be able to get the field 
> name regardless of the order I select them in the select statement.
> 
> Sean


Hi Sean,

Here is a idea of the code that we would use to read column names and
values
here. Note it's untested in its current form, but should give you an
idea of
how to go about it.


<?

$dbstring = "host=x user=x password=x dbname=x";
$conn = pg_connect($dbstring);

$sql = "SELECT 'test1' AS string1, 'test2' AS string2";
$res = pg_query($conn, $sql);

for ($i = 0; $i < pg_num_rows($res); $i++)
{
	$row = pg_fetch_array($res, $i, PGSQL_ASSOC);

	foreach ($row as $field => $value)
	{
		echo "fieldname: " . $field . "<br>";
		echo "value    : " . $value . "<br>\n";
	}
}

?>


Kind regards,

Mark.

------------------------
WebBased Ltd
17 Research Way
Plymouth
PL6 8BT

T: +44 (0)1752 797131
F: +44 (0)1752 791023

http://www.webbased.co.uk   
http://www.infomapper.com
http://www.swtc.co.uk  

This email and any attachments are confidential to the intended
recipient
and may also be privileged. If you are not the intended recipient please
delete it from your system and notify the sender. You should not copy it
or
use it for any purpose nor disclose or distribute its contents to any
other
person.


_______________________________________________
postgis-users mailing list
postgis-users at postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users



More information about the postgis-users mailing list