<data>
<user id="..." login="..." realname="...">
<items>
<img item_1="...">...</item_1>
<item_2 img="...">...</item_2>
</items>
</user>
<user id="..." login="..." realname="...">
<items>
<img item_1="...">...</item_1>
<item_2 img="...">...</item_2>
</items>
</user>
</data>
CREATE TABLE user (
id INT PRIMARY KEY NOT NULL,
login VARCHAR(40) NOT NULL,
realname VARCHAR(40) NOT NULL,
item_1 VARCHAR(40) NOT NULL
item_2 VARCHAR(40) NOT NULL
);
LOAD XML INFILE 'file.xml'
REPLACE INTO TABLE user
ROWS IDENTIFIED BY '<user>';
</user>
to change it impossible, this API with third-party serviceload the xml takes a file from the local FS that hinders (in one way or another) to treat CML (even in the console of the regular season by cutting all unnecessary item)?
is it possible to do it within the framework of such request removal from the database those records whose IDs are not found in the XML file?Directly in any way. You can importnat data from XML into a temporary table and on the basis of ID appeared to carry all the excess from the primary (delete from main table where id not in (select from table tempforxml))
The question is in how you can retrieve data from item_1 located in the img attribute. When I create the table field with the name "img", there are parsed from the data attribute of the last item tag and the duplication of field names in MySQL are not allowed, and attribute names cannot be changed. If anyone knows how to access the attribute through a LOAD of XML, please suggest.Directly it is impossible. People with distorted load data infile with follow-up work with xml in the database, type this newtover.tumblr.com/post/14858246616/mysql-load-data-from-xml. But this is unlikely to be effective way.
Here's Your XML-ku handler in PHP.
Checks for an existing database ID and Login. If TRUE - does not make.
mysql_connect("localhost", "xmluploader", "xmluploader") or die(mysql_error());
mysql_select_db("xmluploader") or die(mysql_error());
$xmlURL = "xml.xml";
$sxml = simplexml_load_file($xmlURL);
foreach($sxml->user as $user) {
$id = stripslashes($user->attributes()['id']);
$login = stripslashes($user->attributes()['login']);
$realname = stripslashes($user->attributes()['realname']);
$item_1 = stripslashes($user->items->item_1->attributes()['img']);
$item_2 = stripslashes($user->items->item_2->attributes()['img']);
$query = mysql_query("SELECT COUNT(*) FROM users WHERE login='$login' OR id='$id'") or die(mysql_error());
$user = mysql_fetch_row($query);
$total = $user[0];
if ("$total" == 0) {
$sql = ("INSERT INTO users (id,login, realname, item_1, item_2) VALUES('$id','$login', '$realname', '$item_1', '$item_2')");
$result = mysql_query($sql) or die("Error ".mysql_error());
} else {
echo 'ID or Login exist!';
}
}
And another question, is it possible to do it within the framework of such request removal from the database those records whose IDs are not found in the XML file?
That's actually do replace this:
if ("$total" == 0) {
$sql = ("INSERT INTO users (id,login, realname, item_1, item_2) VALUES('$id','$login', '$realname', '$item_1', '$item_2')");
$result = mysql_query($sql) or die("Error ".mysql_error());
} else {
echo 'ID or Login exist!';
}
this:
if ("$total" == 0) {
$sql = ("INSERT INTO users (id,login, realname, item_1, item_2) VALUES('$id','$login', '$realname', '$item_1', '$item_2')");
$result = mysql_query($sql) or die("Error ".mysql_error());
} else {
$q = "DELETE FROM users users WHERE users.id != '$id'";
mysql_query($q);
header("Location: index.php");
}
PS Sorry for the spam :) - (please edit your post :) )
Find more questions by tags MySQL