Updating MySQL Using XML


Over the last few weeks I have set up a PHP script which sorts and displays my test database and queries the data by various strings so people can search for products by description and price etc.

However, I am still stuck at the stage of uploading data from an XML file to my database.

I am quite puzzled because I have added an else statement (else{echo (“This does not work </br>”);}) which runs twice. I cant understand why this runs twice as there is only one query.

Can anyone give me any advice or suggestions on why this runs twice or how I can get this code to work.

----Code so far---->

$xmlReader = new XMLReader();

$filename = “datafeed_98057.xml”;
$url = “http://www.domain.co.uk/datafeed.xml”;

file_put_contents($filename, file_get_contents($url));


while ($xmlReader->read())


switch ($xmlReader->name)



$dom = new DOMDocument();
$domNode = $xmlReader->expand();
$element = $dom->appendChild($domNode);
$domString = utf8_encode($dom->saveXML($element));
$prod = new SimpleXMLElement($domString);

$id = $prod->prod[‘id’];
$description = $prod->name;
$image = $prod->awImage;
$fulldescription = $prod->desc;

//insert query
if(strlen($prod) > 0)

$query = mysql_query("REPLACE INTO productfeed
(id, description, fulldescription, image)
VALUES (‘$id’,‘$description’,‘$image’,‘$fulldescription’) ");
echo $id . “has been inserted </br>”;
else{echo (“This does not work </br>”);}

Could you post the xml as well please?

Don’t recognise the mysql_query syntax so not surprised that it is going to the ‘else’ bit but it will run twice if there is more than one name=prod in the xml.