简体   繁体   中英

How can I place this xml data into mySQL using PHP

I am trying to write the channels xml data into my SQL table in the data based on this xml data provided below. I am trying to do the same to the programme table using the xml file. I was wondering is there a way of doing the insert using PHP if so please help me.

<tv generator-info-name="xmltv.co.uk" source-info-name="xmltv.co.uk">
  <channel id="0052a71acac348ff93f5680aa9c125eb">
    <display-name>2910</display-name>
  </channel>
  <channel id="00da025711e82cf319cb488d5988c099">
    <display-name>Sony Movies</display-name>
  </channel>
  <channel id="00daa0e366e2f29473f9112302912ff6">
    <display-name>Anytime</display-name>
  </channel>
  <channel id="00dfea977320f17bb419abaa1f079f39">
    <display-name>Good Food</display-name>
    <icon src="/images/channels/00dfea977320f17bb419abaa1f079f39.png"/>
  </channel>
<programme start="20200323234000 +0000" stop="20200324003000 +0000" channel="ffd49fe9acd778774b4933e10b6afb75">
    <title lang="en">All Elite Wrestling: Dynamite</title>
    <desc lang="en">Action includes the reveal of the Exalted One for the Dark Order. Plus, Best Friends take on Lucha Bros. S1 Ep24</desc>
    <episode-num system="onscreen">s01.e24</episode-num>
  </programme>
  <programme start="20200323123000 +0000" stop="20200323133000 +0000" channel="ffd49fe9acd778774b4933e10b6afb75">
    <title lang="en">Loose Women</title>
    <desc lang="en">Join the Loose Women panellists as they interview celebrity guests and discuss the day's most topical issues, from current affairs to celebrity gossip and the latest showbiz news.</desc>
  </programme>
  <programme start="20200323100000 +0000" stop="20200323123000 +0000" channel="ffd49fe9acd778774b4933e10b6afb75">
    <title lang="en">This Morning</title>
    <desc lang="en">The This Morning team bring you top celebrity guests, lifestyle advice, stunning competitions and award-winning features, all topped off with a sprinkle of humour for good measure.</desc>
  </programme>
</tv>

Hello and welcome as a new member.

As others have pointed out, phpMyAdmin is a tool for communicating with MySQL which is the database you use.

We can't write the whole code for you, but I want to help you along the way. Replace "display-name" with dispayname "all places in your xml file.

After that you can use SimpleXML which is part of php. After you finish this, you will need to create a query for SQL in php that inserts these rows.

<?php

include 'xml.php';

$xml = new SimpleXMLElement($xmlstr);

function GetTVChannels($xml) {
        $TvChannels=[];

        $i=0;
        foreach ($xml->channel as $channel) {
            $TvChannels[$i]["displayname"]=$channel->displayname;
            $i=$i+1;
        }
        return $TvChannels;
}


function GetTVTvProgrammes($xml) {
    $TvProgrammes=[];

    $i=0;
    foreach ($xml->programme as $programme) {
        $TvProgrammes[$i]["title"]=$programme->title;
        $TvProgrammes[$i]["desc"]=$programme->desc;
        $TvProgrammes[$i]["episode"]=$programme->episode;

        $i=$i+1;
    }
    return $TvProgrammes;
}

GetTVChannels($xml); // Get array of all TVChannels
GetTVTvProgrammes($xml); // Get array of all TVTvProgrammes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM