Hoping for some assistance here. I have a large xml file (called xml-file.xml in the code above). The problem I am having with the code above is that it is very slow to write everything to the database. I think the reason it is slow is because of the section that loops through the movies and writes only the movie name in a comma separated string to the table. My question – is there a more efficient way to convert the movie names to a comma separated string and then write to the table? Thanks in advance for any help!
The XML Markup:
<users>
<user>
<id>7280462</id>
<name>John</name>
<movies>
<name>
<![CDATA[Jack]]>
</name>
<description>
<![CDATA[comedy]]>
</description>
<name>
<![CDATA[Superman]]>
</name>
<description>
<![CDATA[action]]>
</description>
<name>
<![CDATA[Ace Venture]]>
</name>
<description>
<![CDATA[comedy]]>
</description>
<name>
<![CDATA[Major League]]>
</name>
<description>
<![CDATA[sports]]>
</description>
</movies>
</user>
</users>
My PHP code:
$file = 'xml-file.xml';
$users = simplexml_load_file($file);
$num_rows = count($users);
for ($j=0; $j < $num_rows; $j++) {
$userid = $users->user[$j]->id;
$name = $users->user[$j]->name;
//update table with userid and name
$db->exec("INSERT INTO table (userid, name) VALUES ('$userid', '$name')");
//loop through movies and write only movie name to table as comma separated string
foreach ($users->user$j]->movies as $element) {
foreach($element as $key => $val) {
if ($key != 'description') {
//echo "{$key}: {$val}";
$title = trim($val).',';
$db->exec("UPDATE table set movies = concat(movies,'','$title') where userid = '$userid'");
}
}
}
}
How about this:
It’s generating all the SQL in one go, so you just need a single statement.