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

alex bodnaru alexbodn at 012.net.il
Tue Feb 21 18:05:50 PST 2006


hi,

pg_query may also put the result in an associative array, so that if you
know the column names, you could subscript the array by name.

alex

Sean M. Montague wrote:
> 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
> _______________________________________________
> 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