PHP save xml and insert into MySQL db

grdangel

New Member
There is a remote XML file out in the internet that I want to be able to download and then write it to a MySQL db for use with a mobile application. You can see the xml page here: https://www.dropbox.com/s/zn5c468dnftoh8m/fire.xml. Each is a different 9-1-1 call and the xml file is constantly changing as there are always 9-1-1 calls coming in. The php file will be executed using a cron job at a desired time interval. So, at the beginning of the script, there will have to be a check for the , to see if it is already in the db, if so, skip, and go on to next call or . If there is no db entry with that , enter the call and precede to the next. Any help would be greatly appreciated! Thanks. Below is the code I am using and I get a syntax error, You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'call, county, id, location, callcreated, station, units, calltype, lat, lng) VAL' at line 1.\[code\]<?phpmysql_connect("localhost", "test", "test") or die(mysql_error());mysql_select_db("firecom") or die(mysql_error());$data = http://stackoverflow.com/questions/13806962/file_get_contents("http://208.71.205.35/PITS/");//thanks WCCCA!$pattern = "/id=\"hidXMLID\" value=http://stackoverflow.com/"([^\"]+)\"/";//looking for the rnd xml id#preg_match_all($pattern, $data, $xmlext);$url = "http://208.71.205.35/PITS/xml/fire_data_" . $xmlext[1][0] . ".xml";//putting together the secret xml url$xml = simplexml_load_file($url);foreach ($xml->marker as $element) {$lat = $element->attributes()->lat;$lng = $element->attributes()->lng;$countydirty = $element->AGENCY;// gets agency$wcccanumberdirty = $element->CALL_NO;$iddirty = $element->TWO_DIGIT_CALL_NO;// gets call id#$calldirty = $element->CALL_TYPE_FINAL_D;// gets call type$locationdirty = $element->LOCATION;// gets location$callcreateddirty = $element->CALL_CREATED_DATE_TIME;$stationdirty = $element->BEAT_OR_STATION;// get first marker station$unitsdirty = $element->UNITS;// get first marker units$calltypedirty = $element->TYPE; //this next section removes the "~" from the start of all the lines$county = str_replace('~','',$countydirty);$wcccanumber = str_replace('~','',$wcccanumberdirty);$id = str_replace('~','',$iddirty);$call = str_replace('~','',$calldirty);$location = str_replace('~','',$locationdirty);$callcreated = str_replace('~','',$callcreateddirty);$station = str_replace('~','',$stationdirty);$units = str_replace('~','',$unitsdirty);$calltype = str_replace('~','',$calltypedirty);$result = mysql_query("SELECT * FROM calls WHERE wcccanumber ='$wcccanumber'") or die(mysql_error());$num_rows = mysql_num_rows($result);if ($num_rows >= 1) { mysql_query("UPDATE calls SET `call` = '$call', location = '$location', units = '$units' WHERE wcccanumber = '$wcccanumber'") or die(mysql_error());}if ($num_rows == 0) { mysql_query("INSERT INTO calls (wcccanumber, `call`, county, id, location, callcreated, station, units, calltype, lat, lng) VALUES('$wcccanumber', '$call', '$county', '$id', '$location', '$callcreated', '$station', '$units', '$calltype', '$lat', '$lng')") or die(mysql_error()); }echo "$call - $county - $wcccanumber - $id - $location - $callcreated - $station - $units - $calltype <br />";}?>\[/code\]
 
Back
Top