Jump to content

Insert XML into mySQL using PHP


rcbmalta

Recommended Posts

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

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.