Jump to content

Updating MySQL From XML


justlukeyou

Recommended Posts

Hi,

 

Does anyone have any experience of using PHP to transfer data from an XML file to a MySQL database?

 

For the last 5 weeks I have been trying to get the following code to work but I am still unable to do it.

 

The code does copy the file from my root folder into another (I eventually plan to use external download onto my server).  However, it comes up with the following error:

 

"Cannot instantiate non-existent class: xmlreader in" which refers to this: $xmlReader = new XMLReader();

 

Does anyone have any experience of transferring data from an XML file to a database? I am using PHP 5 so I dont know why this doesn't work.

 

<?php

ini_set('display_errors', 1);
error_reporting(-1);

$host="hostname"; //  
$username="username"; //  
$password="password"; // 
$db_name="db"; //  
$tbl_name="productfeed"; //  

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or ("no connection"); 
mysql_select_db("$db_name")or die("Database Connection Error");

$xmlReader = new XMLReader();

$filename = "datafeed_98057.xml";
$url = "[url=http://www.ukhomefurniture.co.uk/datafeed.xml]http://www.ukhomefurniture.co.uk/datafeed.xml[/url]";

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

$xmlReader->open($filename);

while ($xmlReader->read())

{

switch ($xmlReader->name)

{

case'prod':

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

$product_code = $product->prod['id'];

$image = $product->awImage;

//insert query
if(strlen($product) > 0)
{
$query = mysql_query("REPLACE INTO productfeed
(image)
VALUES ('$awImage')");
echo $awImage . "has been inserted </br>";
die('yes it works'); 

error_reporting(E_ALL);
if (ini_get('display_errors')) {
    ini_set('display_errors', 1);
}
}
break;
}
}
?>

 

MOD EDIT: DB credentials removed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.