Importing XML and Skipping Errors (PHP)



I have a CRON job that runs each night to update our stock system (from an external source) via multiple XML files.


However, as is occasionally the case, errors appear in the XML (nothing I can do about this - 3rd party provides this).


When an error does occur, the script comes to a stop and no more files are processed. Is there a way that if an error is encountered, the remaining files are still imported?


Here is my code:



<?php include 'connect.php';


$sql = "DELETE FROM stock WHERE allow_keep!='1'";
mysql_query($sql);

//clear stock table then loop through and add all active users


$query = "SELECT * from users where active='1'";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){


$xml = simplexml_load_file(“users/XML_FILE_”.$row['id'].".xml");

foreach($xml->children() as $child)
{


$myquery="REPLACE into stock set
StockID='" . $child->stockid .
"', Make='" . addslashes($child->make) .
"', Model='" . addslashes($child->model) .
"', image='" . $child->image_url_1 .
"', details='" . addslashes($child->description) .
"', price='" . $child->price .
"'";

$result2=mysql_query($myquery) or die(mysql_error());




}

}


?>


Appreciate any help I can get with this!


No comments:

Post a Comment