<?
/*reference ADOdb classes*/
include('adodb/adodb.inc.php');

/*output folder for generating shapefiles*/
$folder4Extraction="C:\\FreeGIS\\tasks\\output";

/*creates a new connection object*/
$dbPostGIS= ADONewConnection('postgres');
/*connects to PostgreSQL server*/
$dbPostGIS->Connect('my_postgresql_server','myuser','mypassword','db_postgis1');

if($dbPostGIS){// database connected!

   /*This SQL could be something like:
     SELECT distinct city_name FROM municipalities*/
   $strSQL="SELECT distinct forest_farm_code FROM forestry_stands";

   $rsExportSourceData=$dbPostGIS->Execute($strSQL);

   if($rsExportSourceData){//recordset opened!

    /*get the total of rows to process*/
       $iTotalRecords=$rsExportSourceData->RecordCount();
       for($i=0;$i<($iTotalRecords);$i++){

           $strExportCommand="ogr2ogr -where \"forest_farm_code='".
                    $rsExportSourceData->Fields("forest_farm_code").
                    "'\" -t_srs EPSG:4326 -f \"ESRI Shapefile\" ".
                    "$folder4Extraction".
                    " PG:\"host=my_postgresql_server user=myuser password=mypassword dbname=db_postgis1\"".
                    " -nln ".$rsExportSourceData->Fields("forest_farm_code").
                    " forestry_stands\n";
           echo$strExportCommand;
           $rsExportSourceData->MoveNext();
       }
   }
}
?>