= MS4W Version 5.x Query From .MAP File Sample = original source: https://gist.github.com/andrewteg/81b8d45948d67003f5f1170e51d66e61 {{{ #!php query sample
getLayer(1);					//second layer in mapfile
$layer->setFilter($filter_string);				//filter results
$layer->template = true;					//for historical reasons, the query attribute must be non-NULL for a layer to be queryable RE: https://www.mapserver.org/mapscript/mapscript.html#layerobj-attributes

$ext = $layer->getExtent();
$query = $layer->queryByRect($map, $ext);			//must query before getting $layer numitems
$numResults = $layer->getNumResults();

$layername = $layer->name;
$layeritems = $layer->numitems;					//count of items in attribute table
//echo 'Layer '.$layername.' has '.$layeritems.' items
'; for ($x=0;$x<$layeritems;$x++) { $layer_headers[$x] = $layer->getItem($x); //for the layer, this gets the attribute field names } //print_r($layer_headers); if($query == MS_SUCCESS) { echo 'Query has '.$layer->getNumResults().' Results:
'; if($layer->getNumResults()>0) { for ($i=0; $i < $layer->getNumResults(); $i++) { $result = $layer->getResult($i); //print_r($result); if($result === FALSE) { error_log('No result '.$i); } else { $shape = $layer->getShape($result); //print_r($shape); //echo ("#$i OBJECTID = ".$shape->values['OBJECTID'].'
'); //ms4w 3.x //echo 'vals='.$shape->numvalues; echo ("#$i OBJECTID = ".$shape->getValue(0)); //ms4w 4.x $vals = array(); echo '
'; for ($x=0;$x<$shape->numvalues;$x++) { //echo $x.'='.$shape->getValue($x).'
'; $vals[$layer_headers[$x]] = $shape->getValue($x); } print_r($vals); //echo 'WKT='.$shape->toWKT().'
'; print_r($shape->values); echo '
'; } } } } else { echo('Query Returned No Results'); } echo '
END
'; ?>
}}}