The below is a sample of an xml file which I am working on :
<?xml version="1.0" encoding="UTF-8" ?>
<BroadcastData creationDate="20140326085217">
<ScheduleData>
<ChannelPeriod beginTime="20140326090000" endTime="20140402044500">
<ChannelId>Rai Uno</ChannelId>
<Event beginTime="20140326090000" duration="1800">
<EventId>260852180006</EventId>
<EventType>P</EventType>
<EpgProduction>
<EpgText language="eng">
<Name>Unomattina storie vere</Name>
</EpgText>
</EpgProduction>
</Event>
<Event beginTime="20140326093000" duration="1500">
<EventId>260852180007</EventId>
<EventType>P</EventType>
<EpgProduction>
<EpgText language="eng">
<Name>Unomattina Verde</Name>
</EpgText>
</EpgProduction>
</Event>
I am trying to parse this XML and post it into a database with the following PHP:
<?php
// Create connection
$con=mysqli_connect("localhost","test","test","epg");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$completeurl ='test.xml';
$doc = simplexml_load_file($completeurl);
foreach ($doc->ScheduleData->ChannelPeriod as $channelPeriod) {
$channelId = (string)$channelPeriod->ChannelId;
foreach ($channelPeriod->Event as $event) {
$beginTime = $event['beginTime'];
foreach ($channelPeriod->Event as $name) {
$programName = $name->EpgProduction->EpgText->Name;
/*printf('<p>Channel: %s<br />Begin Time: %s<br />Program Name: %s</p>', $channelId, $beginTime, $programName);*/
}
}
}
$sql = "insert into `epg` (`ChannelId`, `BeginTime`, `ShortName`) values ('$channelId', '$beginTime', '$programName')";
$perform_insert = mysqli_query($con,$sql) or die("<b>Data could not be entered</b>.\n<br />Query: <br />\nError: (" . mysqli_errno($con) . ") " . mysqli_error($con));
if (mysqli_query($con,$perform_insert))
{
echo "Database updated successfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
?>
For some reason, this is not happening and I have no idea what am I doing wrong, can you kindly have a look please, many thanks for your help