<?php
include '/var/www/html/api/db-params.php';
$f=fopen("/root/pru.TranCorp.xml", "r");
$c=0;
mysql_connect($DBHost, $DBUser, $DBpw)
     or die('Could not connect: ' . mysql_error()); 
mysql_select_db($DB) or die('Could not select database');
$m=0;
function f($x){
  return mysql_real_escape_string($x);
  //return $x;
}

while (!feof($f)) {
  $l = fgets($f);
  if(substr($l,0,10)=='<property>'){
    $c++;
    $len=strlen($l);
    if($len>$m){
      $m = $len;
      echo "line ".$c."=".$m."\n";
    }
    $xml = new SimpleXMLElement($l); 
    $pid=$xml->details->{'propid'};
    $query = sprintf("delete from propertyinfo where propid = '%s'",
                     f($pid)); 
    $query = sprintf("insert into propertyinfo
              (update_timestamp,street_address, city_name, zipcode, state_code, county, subdivision, neighborhood_name, longitude, latitude, directions, price, year_built, num_bedrooms, num_full_bathrooms, num_half_bathrooms, lot_size, square_feet, date_listed, property_type, description, mlsId, mlsName, provider_listingid, pruid, propid, listing_type, status, picture_url)
              values(now(),'%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',
                     '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s',
                     '%s','%s','%s','%s','%s','%s','%s','%s')",
                     f($xml->location->{'street-address'}),
                     f($xml->location->{'city-name'}),
                     f($xml->location->{'zipcode'}),
                     f($xml->location->{'state-code'}),
                     f($xml->location->{'county'}),
                     f($xml->location->{'subdivision'}),
                     f($xml->location->{'neighborhood-name'}),
                     f($xml->location->{'longitude'}),
                     f($xml->location->{'latitude'}),
                     f($xml->location->{'directions'}),
                     f($xml->details->price),
                     f($xml->details->{'year-built'}),
                     f($xml->details->{'num-bedrooms'}),
                     f($xml->details->{'num-full-bathrooms'}),
                     f($xml->details->{'num-half-bathrooms'}),
                     f($xml->details->{'lot-size'}),
                     f($xml->details->{'square-feet'}),
                     f($xml->details->{'date-listed'}),
                     f($xml->details->{'property-type'}),
                     f($xml->details->{'description'}),
                     f($xml->details->{'mlsId'}),
                     f($xml->details->{'mlsName'}),
                     f($xml->details->{'provider-listingid'}),
                     f($xml->details->{'pruid'}),
                     f($xml->details->{'propid'}),
                     f($xml->{'listing-type'}),
                     f($xml->status),
                     f($xml->pictures->picture->{'picture-url'}));

    if($c % 1000 ==0){
      echo 'after '. $c . " memory=". memory_get_usage()."\n";
    }
  }
 }
echo "memory:".memory_get_usage();
?>
