Jump to content

Insert XML into mySQL using PHP


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">
<ChannelPeriod beginTime="20140326090000" endTime="20140402044500">
<ChannelId>Rai Uno</ChannelId>
<Event beginTime="20140326090000" duration="1800">
<EpgText language="eng">
<Name>Unomattina storie vere</Name>
<Event beginTime="20140326093000" duration="1500">
<EpgText language="eng">
<Name>Unomattina Verde</Name>

I am trying to parse this XML and post it into a database with the following PHP:

// Create connection

// 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";
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. Are you sure you have something important to add to it?

Join the conversation

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

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.