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

Sean M. Montague smontague at ATSDenver.com
Tue Feb 21 10:56:27 PST 2006


I don't want to worry which order I select the fields.  I select them
how I want them displayed, not in the order they are in the db. I did
actually implement what Brent suggested, but now I just want to learn
more about pg_fetch_array and what not.  I did use pg_field_name, but I
would have to order the fields in the same order as the db.  Below is
what I'm doing now, and it works fine.


$flds=('store_number,lastname,firstname,city,state,zip,effective_date,bi
rthdate,test_kitchen_score,test_front_score,shirt_size,idx');

$sql = ("SELECT $flds From $table ORDER BY lastname ASC");	
$ATeam=pg_exec($conn, $sql);

$fldsArray = explode(",", $flds);
if($ATeam){
	$rows=pg_numrows($ATeam);
	for($i=0; $i<$rows; $i++){
		for($t=0; $t<count($data)-1; $t++){
			$anchor=$anchorsArray[$t];
			$name=$fldsArray[$t];
		}
	}
}

Sean

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

I'm missing the point as to why you're doing this.  In any case, you
might
be interested in pg_field_name:

http://us3.php.net/manual/en/function.pg-field-name.php

How do you not know the field names?  Are you running a 'select *'?

-Don

-----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: Tuesday, February 21, 2006 11:51 AM
To: PostGIS Users Discussion
Subject: RE: [postgis-users] Identifying field names in select out of
order

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


_______________________________________________
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