Save simplexmlelement object to mysql

Hi i have this code can any one tell me how to save this on mysql, also if someone can tell me how to create colums from the result ex. “TEAM” “TEAM ID” etc. Please some help

<?php
 class XMLSoccer{
    protected $service_url="http://www.xmlsoccer.com/FootballData.asmx";
    protected $api_key,$request_ip;

    const TIMEOUT_GetLiveScore=25;
    const TIMEOUT_GetLiveScoreByLeague=25;
    const TIMEOUT_GetOddsByFixtureMatchID=3600;
    const TIMEOUT_GetHistoricMatchesByLeagueAndSeason=3600;
    const TIMEOUT_GetAllTeams=3600;
    const TIMEOUT_GetAllTeamsByLeagueAndSeason=3600;
    const TIMEOUT_Others=300;
    const TIMEOUT_CURL=30;

    public function __construct($api_key=""){
        $this->request_ip=gethostbyname(gethostname());
        if(empty($this->service_url)) throw new XMLSoccerException("service_url cannot be empty. Please setup");
        if(!empty($api_key)) $this->api_key=$api_key;
        if(empty($this->api_key)) throw new XMLSoccerException("api_key cannot be empty. Please setup.");
    }


    /*
        list available methods with params.
    */
    public function __call($name,$params){
        $data=$this->request($this->buildUrl($name,$params));
        if(false===($xml = simplexml_load_string($data))) throw new XMLSoccerException("Invalid XML");
        if(strstr($xml[0],"To avoid misuse of the service")){
            switch($name){
                case "GetLiveScore":
                case "GetLiveScoreByLeague":
                case "GetOddsByFixtureMatchID":
                case "GetHistoricMatchesByLeagueAndSeason":
                case "GetAllTeams":
                case "GetAllTeamsByLeagueAndSeason":
                    throw new XMLSoccerException($xml[0],constant("self::TIMEOUT_".$name));
                default:
                    throw new XMLSoccerException($xml[0],self::TIMEOUT_Others);
            }
        }
        return $xml;
    }


    protected function buildUrl($method,$params){
        $url=$this->service_url."/".$method."?apikey=".$this->api_key;
        for($i=0;$i<count($params);$i++){
            if(is_array($params[$i])){
                foreach($params[$i] as $key=>$value){
                    $url.="&".strtolower($key)."=".rawurlencode($value);
                }
            }
            else{
                throw new XMLSoccerException("Arguments must be an array");
            }
        }
        return $url;
    }


    protected function request($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, self::TIMEOUT_CURL);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, self::TIMEOUT_CURL);
        curl_setopt($curl, CURLOPT_INTERFACE,$this->request_ip);
        $data = curl_exec($curl);
        $cerror=curl_error($curl);
        $cerrno=curl_errno($curl);
        $http_code=curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);

        if($cerrno!=0) throw new XMLSoccerException($cerror,E_USER_WARNING);

        if($http_code == 200 ) return $data;
        throw new XMLSoccerException($http_code .' - '. $data . "\nURL: " . $url);
    }

    public function setRequestIp($ip){
        if(empty($ip)) throw new XMLSoccerException("IP parameter cannot be empty",E_USER_WARNING);
        $this->request_ip=$ip;
    }

    public function setServiceUrl($service_url){
        if(empty($service_url)) throw new XMLSoccerException("service_url parameter cannot be empty",E_USER_WARNING);
        $this->service_url=$service_url;
    }


}
class XMLSoccerException extends Exception{

}
try{
    $soccer=new XMLSoccer("your_key");
    $soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
    $result=$soccer->GetLeagueStandingsBySeason(array("league"=>3,"seasonDateString"=>"1314"));
    print_r($result);
}
catch(XMLSoccerException $e){
    echo "XMLSoccerException: ".$e->getMessage();
}
?>

The result is this

SimpleXMLElement Object ( [TeamLeagueStanding] => Array ( [0] => SimpleXMLElement Object ( [Team] => Celtic [Team_Id] => 54 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 31 [Draw] => 6 [Lost] => 1 [NumberOfShots] => 840 [YellowCards] => 44 [RedCards] => 2 [Goals_For] => 102 [Goals_Against] => 25 [Goal_Difference] => 77 [Points] => 99 ) [1] => SimpleXMLElement Object ( [Team] => Motherwell [Team_Id] => 47 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 22 [Draw] => 4 [Lost] => 12 [NumberOfShots] => 556 [YellowCards] => 59 [RedCards] => 2 [Goals_For] => 64 [Goals_Against] => 60 [Goal_Difference] => 4 [Points] => 70 ) [2] => SimpleXMLElement Object ( [Team] => Aberdeen [Team_Id] => 45 [Played] => 38 [PlayedAtHome] => 18 [PlayedAway] => 20 [Won] => 20 [Draw] => 8 [Lost] => 10 [NumberOfShots] => 548 [YellowCards] => 50 [RedCards] => 4 [Goals_For] => 53 [Goals_Against] => 38 [Goal_Difference] => 15 [Points] => 68 ) [3] => SimpleXMLElement Object ( [Team] => Dundee United [Team_Id] => 51 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 16 [Draw] => 10 [Lost] => 12 [NumberOfShots] => 610 [YellowCards] => 49 [RedCards] => 3 [Goals_For] => 65 [Goals_Against] => 50 [Goal_Difference] => 15 [Points] => 58 ) [4] => SimpleXMLElement Object ( [Team] => Inverness C [Team_Id] => 48 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 16 [Draw] => 9 [Lost] => 13 [NumberOfShots] => 524 [YellowCards] => 55 [RedCards] => 2 [Goals_For] => 44 [Goals_Against] => 44 [Goal_Difference] => 0 [Points] => 57 ) [5] => SimpleXMLElement Object ( [Team] => St Johnstone [Team_Id] => 46 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 15 [Draw] => 8 [Lost] => 15 [NumberOfShots] => 514 [YellowCards] => 59 [RedCards] => 6 [Goals_For] => 48 [Goals_Against] => 42 [Goal_Difference] => 6 [Points] => 53 ) [6] => SimpleXMLElement Object ( [Team] => Ross County [Team_Id] => 360 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 11 [Draw] => 7 [Lost] => 20 [NumberOfShots] => 486 [YellowCards] => 65 [RedCards] => 5 [Goals_For] => 44 [Goals_Against] => 62 [Goal_Difference] => -18 [Points] => 40 ) [7] => SimpleXMLElement Object ( [Team] => St Mirren [Team_Id] => 56 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 10 [Draw] => 9 [Lost] => 19 [NumberOfShots] => 476 [YellowCards] => 47 [RedCards] => 2 [Goals_For] => 39 [Goals_Against] => 58 [Goal_Difference] => -19 [Points] => 39 ) [8] => SimpleXMLElement Object ( [Team] => Kilmarnock [Team_Id] => 52 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 11 [Draw] => 6 [Lost] => 21 [NumberOfShots] => 472 [YellowCards] => 55 [RedCards] => 5 [Goals_For] => 45 [Goals_Against] => 66 [Goal_Difference] => -21 [Points] => 39 ) [9] => SimpleXMLElement Object ( [Team] => Partick [Team_Id] => 561 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 8 [Draw] => 14 [Lost] => 16 [NumberOfShots] => 623 [YellowCards] => 67 [RedCards] => 5 [Goals_For] => 46 [Goals_Against] => 65 [Goal_Difference] => -19 [Points] => 38 ) [10] => SimpleXMLElement Object ( [Team] => Hibernian [Team_Id] => 53 [Played] => 38 [PlayedAtHome] => 20 [PlayedAway] => 18 [Won] => 8 [Draw] => 11 [Lost] => 19 [NumberOfShots] => 507 [YellowCards] => 62 [RedCards] => 5 [Goals_For] => 31 [Goals_Against] => 51 [Goal_Difference] => -20 [Points] => 35 ) [11] => SimpleXMLElement Object ( [Team] => Hearts [Team_Id] => 50 [Played] => 38 [PlayedAtHome] => 19 [PlayedAway] => 19 [Won] => 10 [Draw] => 8 [Lost] => 20 [NumberOfShots] => 486 [YellowCards] => 68 [RedCards] => 5 [Goals_For] => 45 [Goals_Against] => 65 [Goal_Difference] => -20 [Points] => 23 ) )

Something like

foreach($xml->TeamLeagueStanding as $entry) {
  echo $entry->team .  " - " . $entry->Team_id;
  }

I’ve never tried it, maybe this page and responses gives you more information : http://stackoverflow.com/questions/19561657/loop-through-an-xml-object-with-simplexml

Once you know how to access each entry, it should be straightforward to insert the values into a database table.

thnx for the replay I changed the code and included your statement in it but it gives to me an error

<?php
include("XMLSoccer.php");
include ("conf.php");
$file_name = basename(__FILE__,'.php');
try{
$soccer=new XMLSoccer($api_key);
$soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
$result=$soccer->GetAllLeagues();
print_r($result);
}
catch(XMLSoccerException $e){
echo "XMLSoccerException: ".$e->getMessage();
}
$table = $file_name;
$query = "SELECT ID FROM " . $table;
$result = mysqli_query($conn,$query);
if(empty($result)) {
echo "<p>" . $table . " table does not exist</p>";
$query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
Id VARCHAR(300),
Name VARCHAR(300),
Country VARCHAR(30),
Historical_Data VARCHAR(300),
Fixtures VARCHAR(300),
Livescore VARCHAR(300),
NumberOfMatches VARCHAR(30),
LatestMatch VARCHAR(300),
IsCup VARCHAR(300)
)");
}
else {
echo "<p>" . $table . "table exists</p>";
} // else
foreach ($results->League as $team) {
$leagueid = $team->Id;
$name = $team->Name;
$country = $team->Country;
$historical_Data = $team->Historical_Data;
$fixtures = $team->Fixtures;
$livescore = $team->Livescore;
$numberOfMatches = $team->NumberOfMatches;
$latestMatch = $team->LatestMatch;
$isCup = $team->IsCup;
}
?>

THE ERROR

GetAllLeagues2table exists

Notice: Undefined variable: results in C:\...\GetAllLeagues2.php on line 34 PHP Notice: Undefined variable: results in C:\...\GetAllLeagues2.php on line 34 PHP Notice: Trying to get property of non-object in C:\...\GetAllLeagues2.php on line 34 PHP Warning: Invalid argument supplied for foreach() in C:\...\GetAllLeagues2.php on line 34

Notice: Trying to get property of non-object in C:.…\GetAllLeagues2.php on line 34

Warning: Invalid argument supplied for foreach() in C:.…\GetAllLeagues2.php on line 34

Process finished with exit code 0

Can you see why?

Your code to retrieve the information:

$result=$soccer->GetAllLeagues();

Your code to do something with it, on line 34 as the error message says:

foreach ($results->League as $team) {

$results is not the same as $result.

i didn’t see that. changed it and now i get this

<?php
include("XMLSoccer.php");
include ("conf.php");
$file_name = basename(__FILE__,'.php');
try{
    $soccer=new XMLSoccer($api_key);
    $soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
    $result=$soccer->GetAllLeagues();
    print_r($result);
}
catch(XMLSoccerException $e){
    echo "XMLSoccerException: ".$e->getMessage();
}
$table = $file_name;
$query = "SELECT ID FROM " . $table;
$result = mysqli_query($conn,$query);
if(empty($result)) {
    echo "<p>" . $table . " table does not exist</p>";
    $query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
        Id VARCHAR(300),
        Name VARCHAR(300),
        Country VARCHAR(30),
        Historical_Data VARCHAR(300),
        Fixtures VARCHAR(300),
        Livescore VARCHAR(300),
        NumberOfMatches VARCHAR(30),
        LatestMatch VARCHAR(300),
        IsCup VARCHAR(300)
    )");
}
else {
    echo "<p>" . $table . "table exists</p>";
} // else
foreach ($result->League as $team) {
    $leagueid = $team->Id;
    $name = $team->Name;
    $country = $team->Country;
    $historical_Data = $team->Historical_Data;
    $fixtures = $team->Fixtures;
    $livescore = $team->Livescore;
    $numberOfMatches = $team->NumberOfMatches;
    $latestMatch = $team->LatestMatch;
    $isCup = $team->IsCup;
}

?>

THE ERROR

GetAllLeagues2table exists

Notice: Undefined property: mysqli_result::$League in C:\...\GetAllLeagues2.php on line 34

Warning: Invalid argument supplied for foreach() in C:.…\GetAllLeagues2.php on line 34
PHP Notice: Undefined property: mysqli_result::$League in C:.…\GetAllLeagues2.php on line 34
PHP Warning: Invalid argument supplied for foreach() in C:.…\GetAllLeagues2.php on line 34

Process finished with exit code 0

Biggest problem is that after you’ve got $result as your xml object, you then use it as the result object from running your SQL query, so that will overwrite your XML object. Change one of them to something else.

$result = $soccer->GetAllLeagues();

then

$result = mysqli_query($conn,$query);

Is the content of the XML object different to the first post now then? I didn’t see a League element in there.

I changed it and now i didnt get any error but there is no table in the database

<?php
include("XMLSoccer.php");
include ("conf.php");
$file_name = basename(__FILE__,'.php');
try{
    $soccer=new XMLSoccer($api_key);
    $soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
    $result=$soccer->GetAllLeagues();
    print_r($result);
}
catch(XMLSoccerException $e){
    echo "XMLSoccerException: ".$e->getMessage();
}
$table = $file_name;
$query = "SELECT ID FROM " . $table;
$second_result = mysqli_query($conn,$query);
if(empty($result)) {
    echo "<p>" . $table . " table does not exist</p>";
    $query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
        Id VARCHAR(300),
        Name VARCHAR(300),
        Country VARCHAR(30),
        Historical_Data VARCHAR(300),
        Fixtures VARCHAR(300),
        Livescore VARCHAR(300),
        NumberOfMatches VARCHAR(30),
        LatestMatch VARCHAR(300),
        IsCup VARCHAR(300)
    )");
}
else {
    echo "<p>" . $table . "table exists</p>";
} // else
foreach ($result->Leagues as $team) {
    $leagueid = $team->Id;
    $name = $team->Name;
    $country = $team->Country;
    $historical_Data = $team->Historical_Data;
    $fixtures = $team->Fixtures;
    $livescore = $team->Livescore;
    $numberOfMatches = $team->NumberOfMatches;
    $latestMatch = $team->LatestMatch;
    $isCup = $team->IsCup;
}

?>

THE FULL RESPOND I GET

D:.…\GetAllLeagues2.php"
SimpleXMLElement Object
(
[League] => Array
(
[0] => SimpleXMLElement Object
(
[Id] => 1
[Name] => English Premier League
[Country] => England
[Historical_Data] => Yes
[Fixtures] => Yes
[Livescore] => Yes
[NumberOfMatches] => 6080
[LatestMatch] => 2016-05-17T21:00:00+00:00
[IsCup] => false
)

        [1] => SimpleXMLElement Object
            (
                [Id] => 2
                [Name] => English League Championship
                [Country] => England
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 8925
                [LatestMatch] => 2016-05-07T13:30:00+00:00
                [IsCup] => false
            )
        [2] => SimpleXMLElement Object
            (
                [Id] => 3
                [Name] => Scottish Premier League
                [Country] => Scotland
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 3648
                [LatestMatch] => 2016-05-15T13:30:00+00:00
                [IsCup] => false
            )
        [3] => SimpleXMLElement Object
            (
                [Id] => 4
                [Name] => Bundesliga
                [Country] => Germany
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 4897
                [LatestMatch] => 2016-05-14T15:30:00+00:00
                [IsCup] => false
            )
        [4] => SimpleXMLElement Object
            (
                [Id] => 5
                [Name] => Serie A
                [Country] => Italy
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 5784
                [LatestMatch] => 2016-05-15T20:45:00+00:00
                [IsCup] => false
            )
        [5] => SimpleXMLElement Object
            (
                [Id] => 6
                [Name] => Serie B
                [Country] => Italy
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 7249
                [LatestMatch] => 2016-05-20T20:30:00+00:00
                [IsCup] => false
            )
        [6] => SimpleXMLElement Object
            (
                [Id] => 7
                [Name] => Ligue 1
                [Country] => France
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 5932
                [LatestMatch] => 2016-05-14T21:00:00+00:00
                [IsCup] => false
            )
        [7] => SimpleXMLElement Object
            (
                [Id] => 8
                [Name] => La Liga
                [Country] => Spain
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 6080
                [LatestMatch] => 2016-05-15T19:30:00+00:00
                [IsCup] => false
            )
        [8] => SimpleXMLElement Object
            (
                [Id] => 9
                [Name] => Superleague Greece
                [Country] => Greece
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 3898
                [LatestMatch] => 2016-04-17T18:00:00+00:00
                [IsCup] => false
            )
        [9] => SimpleXMLElement Object
            (
                [Id] => 10
                [Name] => Eredivisie
                [Country] => Holland
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 4896
                [LatestMatch] => 2016-05-08T14:30:00+00:00
                [IsCup] => false
            )
        [10] => SimpleXMLElement Object
            (
                [Id] => 11
                [Name] => Jupiler League
                [Country] => Belgium
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 4400
                [LatestMatch] => 2016-03-13T18:00:00+00:00
                [IsCup] => false
            )
        [11] => SimpleXMLElement Object
            (
                [Id] => 12
                [Name] => Süper Lig
                [Country] => Turkey
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 4954
                [LatestMatch] => 2016-05-30T00:00:00+00:00
                [IsCup] => false
            )
        [12] => SimpleXMLElement Object
            (
                [Id] => 14
                [Name] => Superliga
                [Country] => Denmark
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 985
                [LatestMatch] => 2016-05-29T17:00:00+00:00
                [IsCup] => false
            )
        [13] => SimpleXMLElement Object
            (
                [Id] => 15
                [Name] => EURO 2012
                [Country] => Europe
                [Historical_Data] => Partial
                [Fixtures] => False
                [Livescore] => False
                [NumberOfMatches] => 31
                [LatestMatch] => 2012-07-01T20:45:00+00:00
                [IsCup] => false
            )
        [14] => SimpleXMLElement Object
            (
                [Id] => 16
                [Name] => Champions League
                [Country] => Europe
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 704
                [LatestMatch] => 2016-05-04T20:45:00+00:00
                [IsCup] => true
            )
        [15] => SimpleXMLElement Object
            (
                [Id] => 17
                [Name] => Europe League
                [Country] => Europe
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 1477
                [LatestMatch] => 2016-05-18T20:45:00+00:00
                [IsCup] => true
            )
        [16] => SimpleXMLElement Object
            (
                [Id] => 18
                [Name] => Primeira Liga
                [Country] => Portugal
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 1070
                [LatestMatch] => 2016-05-15T18:00:00+00:00
                [IsCup] => false
            )
        [17] => SimpleXMLElement Object
            (
                [Id] => 19
                [Name] => Scottish First Division
                [Country] => Scotland
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 2881
                [LatestMatch] => 2016-05-01T13:30:00+00:00
                [IsCup] => false
            )
        [18] => SimpleXMLElement Object
            (
                [Id] => 20
                [Name] => Major League Soccer
                [Country] => USA
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 1017
                [LatestMatch] => 2016-07-14T04:30:00+00:00
                [IsCup] => false
            )
        [19] => SimpleXMLElement Object
            (
                [Id] => 33
                [Name] => Allsvenskan
                [Country] => Sweden
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 815
                [LatestMatch] => 2016-07-11T19:00:00+00:00
                [IsCup] => false
            )
        [20] => SimpleXMLElement Object
            (
                [Id] => 34
                [Name] => FA Cup
                [Country] => England
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 238
                [LatestMatch] => 2016-05-21T18:30:00+00:00
                [IsCup] => false
            )
        [21] => SimpleXMLElement Object
            (
                [Id] => 35
                [Name] => League Cup
                [Country] => England
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 240
                [LatestMatch] => 2016-01-27T20:45:00+00:00
                [IsCup] => false
            )
        [22] => SimpleXMLElement Object
            (
                [Id] => 36
                [Name] => Mexican Primera League
                [Country] => Mexico
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 781
                [LatestMatch] => 2016-05-09T00:00:00+00:00
                [IsCup] => false
            )
        [23] => SimpleXMLElement Object
            (
                [Id] => 37
                [Name] => Brasileirao
                [Country] => Brazil
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 1255
                [LatestMatch] => 2016-07-13T01:30:00+00:00
                [IsCup] => false
            )
        [24] => SimpleXMLElement Object
            (
                [Id] => 38
                [Name] => English League 1
                [Country] => England
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 2209
                [LatestMatch] => 2016-05-08T13:30:00+00:00
                [IsCup] => false
            )
        [25] => SimpleXMLElement Object
            (
                [Id] => 39
                [Name] => English League 2
                [Country] => England
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 2211
                [LatestMatch] => 2016-05-07T16:00:00+00:00
                [IsCup] => false
            )
        [26] => SimpleXMLElement Object
            (
                [Id] => 40
                [Name] => Ukrainian Premier League
                [Country] => Ukraine
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 550
                [LatestMatch] => 2016-05-15T18:30:00+00:00
                [IsCup] => false
            )
        [27] => SimpleXMLElement Object
            (
                [Id] => 41
                [Name] => Russian Football Premier League 
                [Country] => Russia
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 732
                [LatestMatch] => 2016-05-21T12:30:00+00:00
                [IsCup] => false
            )
        [28] => SimpleXMLElement Object
            (
                [Id] => 42
                [Name] => Australian A-League
                [Country] => Australia
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 294
                [LatestMatch] => 2016-04-10T09:00:00+00:00
                [IsCup] => false
            )
        [29] => SimpleXMLElement Object
            (
                [Id] => 43
                [Name] => World Cup 2014
                [Country] => International
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 64
                [LatestMatch] => 2014-07-13T21:00:00+00:00
                [IsCup] => true
            )
        [30] => SimpleXMLElement Object
            (
                [Id] => 44
                [Name] => Tippeligaen
                [Country] => Norway
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 599
                [LatestMatch] => 2016-07-10T18:00:00+00:00
                [IsCup] => false
            )
        [31] => SimpleXMLElement Object
            (
                [Id] => 45
                [Name] => Chinese Super League
                [Country] => China
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 538
                [LatestMatch] => 2016-07-13T13:35:00+00:00
                [IsCup] => false
            )
        [32] => SimpleXMLElement Object
            (
                [Id] => 46
                [Name] => Lega Pro
                [Country] => Italy
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 2040
                [LatestMatch] => 2016-05-08T18:00:00+00:00
                [IsCup] => true
            )
        [33] => SimpleXMLElement Object
            (
                [Id] => 47
                [Name] => 2. Bundesliga
                [Country] => Germany
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 608
                [LatestMatch] => 2016-05-15T15:30:00+00:00
                [IsCup] => false
            )
        [34] => SimpleXMLElement Object
            (
                [Id] => 48
                [Name] => Adelante
                [Country] => Spain
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 922
                [LatestMatch] => 2016-06-04T20:30:00+00:00
                [IsCup] => false
            )
        [35] => SimpleXMLElement Object
            (
                [Id] => 49
                [Name] => Ligue 2
                [Country] => France
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 754
                [LatestMatch] => 2016-05-13T20:45:00+00:00
                [IsCup] => false
            )
        [36] => SimpleXMLElement Object
            (
                [Id] => 50
                [Name] => Superettan
                [Country] => Sweden
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 598
                [LatestMatch] => 2016-07-03T18:00:00+00:00
                [IsCup] => false
            )
        [37] => SimpleXMLElement Object
            (
                [Id] => 51
                [Name] => Brasileirão Série B
                [Country] => Brazil
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 906
                [LatestMatch] => 2016-07-15T02:00:00+00:00
                [IsCup] => false
            )
        [38] => SimpleXMLElement Object
            (
                [Id] => 52
                [Name] => Norwegian 1. Divisjon
                [Country] => Norway
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 615
                [LatestMatch] => 2016-07-11T19:00:00+00:00
                [IsCup] => false
            )
        [39] => SimpleXMLElement Object
            (
                [Id] => 53
                [Name] => Ekstraklasa
                [Country] => Poland
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 478
                [LatestMatch] => 2016-04-09T18:00:00+00:00
                [IsCup] => false
            )
        [40] => SimpleXMLElement Object
            (
                [Id] => 54
                [Name] => Primera Division
                [Country] => Argentina
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 679
                [LatestMatch] => 2016-05-24T02:15:00+00:00
                [IsCup] => false
            )
        [41] => SimpleXMLElement Object
            (
                [Id] => 56
                [Name] => EURO 2016
                [Country] => International
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 48
                [LatestMatch] => 2016-07-10T21:00:00+00:00
                [IsCup] => true
            )
    )
[AccountInformation] => Data requested at 7/15/2016 8:27:26 PM

)

GetAllLeagues2table exists

Process finished with exit code 0

I’m confused by this bit:

$query = "SELECT ID FROM " . $table;
$second_result = mysqli_query($conn,$query);
if(empty($result)) {
    echo "<p>" . $table . " table does not exist</p>";

Did you mean to check after running the query whether your XML was empty? Or were you intending to check whether the result from the query was empty, in which case you’ll have to use:

if(!($second_result)) {

I’m not sure why selecting an ID from a table and getting an empty result leads you to believe that the table doesn’t exist, but that’s a separate matter. But the bit of code that creates the table will never run because you’re checking $result, which is not empty because that’s your XML data.

THANK YOU VERY MUCH FOR THE HELP i managed to work the code, every thing works great except that the in the database table i have only one row in it , and that is the last content from the result (41) . Here is the code

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
$file_name = basename(__FILE__,'.php');
include("XMLSoccer.php");
// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//CHECKING IF TABLE EXIST IF NOT CREATE NEW
$table = $file_name;
$query = "SELECT ID FROM " . $table; 
$resultat = mysqli_query($conn,$query);


if(empty($resultat)) {
    echo "<p>" . $table . " table does not exist</p>";
    $query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
        Id int NOT NULL PRIMARY KEY,
        Name varchar(255) NOT NULL,
        Country varchar(255) NOT NULL,
        Historical_Data varchar(255) NOT NULL,
        Fixtures varchar(255) NOT NULL,
        Livescore varchar(255) NOT NULL,
        NumberOfMatches varchar(255) NOT NULL,
        LatestMatch varchar(255) NOT NULL,
        IsCup varchar(255) NOT NULL
    )");
}
else {
    echo "<p>" . $table . "table exists</p>";
} // else

/////GETING THE DATA FROM SERVICE

try {
    $soccer = new XMLSoccer($api_key);
    $soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
    $result = $soccer->GetAllLeagues();
    print_r($result);
} catch (XMLSoccerException $e) {
    echo "XMLSoccerException: " . $e->getMessage();
}

foreach ($result->League as $team) {
    $leagueid = $team->Id;
    $name = $team->Name;
    $country = $team->Country;
    $historical_Data = $team->Historical_Data;
    $fixtures = $team->Fixtures;
    $livescore = $team->Livescore;
    $numberOfMatches = $team->NumberOfMatches;
    $latestMatch = $team->LatestMatch;
    $isCup = $team->IsCup;
}


///INSERTING DATA INTO THE TABLE
$sql = "INSERT INTO $file_name (Id, Name, Country, Historical_Data, Fixtures, Livescore, NumberOfMatches, LatestMatch, IsCup)
VALUES ('$leagueid', '$name', '$country', '$historical_Data', '$fixtures', '$livescore', '$numberOfMatches', '$latestMatch', '$isCup')on duplicate key update id='$leagueid'";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

The response

testtable exists

SimpleXMLElement Object ( [League] => Array ( [0] => SimpleXMLElement Object ( [Id] => 1 [Name] => English Premier League [Country] => England [Historical_Data] => Yes [Fixtures] => Yes [Livescore] => Yes [NumberOfMatches] => 6080 [LatestMatch] => 2016-05-17T21:00:00+00:00 [IsCup] => false )
        [1] => SimpleXMLElement Object
            (
                [Id] => 2
                [Name] => English League Championship
                [Country] => England
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 8925
                [LatestMatch] => 2016-05-07T13:30:00+00:00
                [IsCup] => false
            )
        [2] => SimpleXMLElement Object
            (
                [Id] => 3
                [Name] => Scottish Premier League
                [Country] => Scotland
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 3648
                [LatestMatch] => 2016-05-15T13:30:00+00:00
                [IsCup] => false
            )
        [3] => SimpleXMLElement Object
            (
                [Id] => 4
                [Name] => Bundesliga
                [Country] => Germany
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 4897
                [LatestMatch] => 2016-05-14T15:30:00+00:00
                [IsCup] => false
            )
        [4] => SimpleXMLElement Object
            (
                [Id] => 5
                [Name] => Serie A
                [Country] => Italy
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 5784
                [LatestMatch] => 2016-05-15T20:45:00+00:00
                [IsCup] => false
            )
        [5] => SimpleXMLElement Object
            (
                [Id] => 6
                [Name] => Serie B
                [Country] => Italy
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 7249
                [LatestMatch] => 2016-05-20T20:30:00+00:00
                [IsCup] => false
            )
        [6] => SimpleXMLElement Object
            (
                [Id] => 7
                [Name] => Ligue 1
                [Country] => France
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 5932
                [LatestMatch] => 2016-05-14T21:00:00+00:00
                [IsCup] => false
            )
        [7] => SimpleXMLElement Object
            (
                [Id] => 8
                [Name] => La Liga
                [Country] => Spain
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 6080
                [LatestMatch] => 2016-05-15T19:30:00+00:00
                [IsCup] => false
            )
        [8] => SimpleXMLElement Object
            (
                [Id] => 9
                [Name] => Superleague Greece
                [Country] => Greece
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 3898
                [LatestMatch] => 2016-04-17T18:00:00+00:00
                [IsCup] => false
            )
        [9] => SimpleXMLElement Object
            (
                [Id] => 10
                [Name] => Eredivisie
                [Country] => Holland
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 4896
                [LatestMatch] => 2016-05-08T14:30:00+00:00
                [IsCup] => false
            )
        [10] => SimpleXMLElement Object
            (
                [Id] => 11
                [Name] => Jupiler League
                [Country] => Belgium
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 4400
                [LatestMatch] => 2016-03-13T18:00:00+00:00
                [IsCup] => false
            )
        [11] => SimpleXMLElement Object
            (
                [Id] => 12
                [Name] => Süper Lig
                [Country] => Turkey
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 4954
                [LatestMatch] => 2016-05-30T00:00:00+00:00
                [IsCup] => false
            )
        [12] => SimpleXMLElement Object
            (
                [Id] => 14
                [Name] => Superliga
                [Country] => Denmark
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 988
                [LatestMatch] => 2016-07-16T18:30:00+00:00
                [IsCup] => false
            )
        [13] => SimpleXMLElement Object
            (
                [Id] => 15
                [Name] => EURO 2012
                [Country] => Europe
                [Historical_Data] => Partial
                [Fixtures] => False
                [Livescore] => False
                [NumberOfMatches] => 31
                [LatestMatch] => 2012-07-01T20:45:00+00:00
                [IsCup] => false
            )
        [14] => SimpleXMLElement Object
            (
                [Id] => 16
                [Name] => Champions League
                [Country] => Europe
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 704
                [LatestMatch] => 2016-05-04T20:45:00+00:00
                [IsCup] => true
            )
        [15] => SimpleXMLElement Object
            (
                [Id] => 17
                [Name] => Europe League
                [Country] => Europe
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 1477
                [LatestMatch] => 2016-05-18T20:45:00+00:00
                [IsCup] => true
            )
        [16] => SimpleXMLElement Object
            (
                [Id] => 18
                [Name] => Primeira Liga
                [Country] => Portugal
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 1070
                [LatestMatch] => 2016-05-15T18:00:00+00:00
                [IsCup] => false
            )
        [17] => SimpleXMLElement Object
            (
                [Id] => 19
                [Name] => Scottish First Division
                [Country] => Scotland
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 2881
                [LatestMatch] => 2016-05-01T13:30:00+00:00
                [IsCup] => false
            )
        [18] => SimpleXMLElement Object
            (
                [Id] => 20
                [Name] => Major League Soccer
                [Country] => USA
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 1024
                [LatestMatch] => 2016-07-17T04:30:00+00:00
                [IsCup] => false
            )
        [19] => SimpleXMLElement Object
            (
                [Id] => 33
                [Name] => Allsvenskan
                [Country] => Sweden
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 817
                [LatestMatch] => 2016-07-16T16:00:00+00:00
                [IsCup] => false
            )
        [20] => SimpleXMLElement Object
            (
                [Id] => 34
                [Name] => FA Cup
                [Country] => England
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 238
                [LatestMatch] => 2016-05-21T18:30:00+00:00
                [IsCup] => false
            )
        [21] => SimpleXMLElement Object
            (
                [Id] => 35
                [Name] => League Cup
                [Country] => England
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 240
                [LatestMatch] => 2016-01-27T20:45:00+00:00
                [IsCup] => false
            )
        [22] => SimpleXMLElement Object
            (
                [Id] => 36
                [Name] => Mexican Primera League
                [Country] => Mexico
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 788
                [LatestMatch] => 2016-07-17T04:00:00+00:00
                [IsCup] => false
            )
        [23] => SimpleXMLElement Object
            (
                [Id] => 37
                [Name] => Brasileirao
                [Country] => Brazil
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 1257
                [LatestMatch] => 2016-07-16T23:30:00+00:00
                [IsCup] => false
            )
        [24] => SimpleXMLElement Object
            (
                [Id] => 38
                [Name] => English League 1
                [Country] => England
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 2209
                [LatestMatch] => 2016-05-08T13:30:00+00:00
                [IsCup] => false
            )
        [25] => SimpleXMLElement Object
            (
                [Id] => 39
                [Name] => English League 2
                [Country] => England
                [Historical_Data] => Yes
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 2211
                [LatestMatch] => 2016-05-07T16:00:00+00:00
                [IsCup] => false
            )
        [26] => SimpleXMLElement Object
            (
                [Id] => 40
                [Name] => Ukrainian Premier League
                [Country] => Ukraine
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 550
                [LatestMatch] => 2016-05-15T18:30:00+00:00
                [IsCup] => false
            )
        [27] => SimpleXMLElement Object
            (
                [Id] => 41
                [Name] => Russian Football Premier League 
                [Country] => Russia
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 732
                [LatestMatch] => 2016-05-21T12:30:00+00:00
                [IsCup] => false
            )
        [28] => SimpleXMLElement Object
            (
                [Id] => 42
                [Name] => Australian A-League
                [Country] => Australia
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 294
                [LatestMatch] => 2016-04-10T09:00:00+00:00
                [IsCup] => false
            )
        [29] => SimpleXMLElement Object
            (
                [Id] => 43
                [Name] => World Cup 2014
                [Country] => International
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 64
                [LatestMatch] => 2014-07-13T21:00:00+00:00
                [IsCup] => true
            )
        [30] => SimpleXMLElement Object
            (
                [Id] => 44
                [Name] => Tippeligaen
                [Country] => Norway
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 602
                [LatestMatch] => 2016-07-16T18:00:00+00:00
                [IsCup] => false
            )
        [31] => SimpleXMLElement Object
            (
                [Id] => 45
                [Name] => Chinese Super League
                [Country] => China
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 544
                [LatestMatch] => 2016-07-16T13:35:00+00:00
                [IsCup] => false
            )
        [32] => SimpleXMLElement Object
            (
                [Id] => 46
                [Name] => Lega Pro
                [Country] => Italy
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 2040
                [LatestMatch] => 2016-05-08T18:00:00+00:00
                [IsCup] => true
            )
        [33] => SimpleXMLElement Object
            (
                [Id] => 47
                [Name] => 2. Bundesliga
                [Country] => Germany
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 608
                [LatestMatch] => 2016-05-15T15:30:00+00:00
                [IsCup] => false
            )
        [34] => SimpleXMLElement Object
            (
                [Id] => 48
                [Name] => Adelante
                [Country] => Spain
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 922
                [LatestMatch] => 2016-06-04T20:30:00+00:00
                [IsCup] => false
            )
        [35] => SimpleXMLElement Object
            (
                [Id] => 49
                [Name] => Ligue 2
                [Country] => France
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 754
                [LatestMatch] => 2016-05-13T20:45:00+00:00
                [IsCup] => false
            )
        [36] => SimpleXMLElement Object
            (
                [Id] => 50
                [Name] => Superettan
                [Country] => Sweden
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 598
                [LatestMatch] => 2016-07-03T18:00:00+00:00
                [IsCup] => false
            )
        [37] => SimpleXMLElement Object
            (
                [Id] => 51
                [Name] => Brasileirão Série B
                [Country] => Brazil
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 913
                [LatestMatch] => 2016-07-17T02:00:00+00:00
                [IsCup] => false
            )
        [38] => SimpleXMLElement Object
            (
                [Id] => 52
                [Name] => Norwegian 1. Divisjon
                [Country] => Norway
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 615
                [LatestMatch] => 2016-07-11T19:00:00+00:00
                [IsCup] => false
            )
        [39] => SimpleXMLElement Object
            (
                [Id] => 53
                [Name] => Ekstraklasa
                [Country] => Poland
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 483
                [LatestMatch] => 2016-07-16T20:30:00+00:00
                [IsCup] => false
            )
        [40] => SimpleXMLElement Object
            (
                [Id] => 54
                [Name] => Primera Division
                [Country] => Argentina
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 679
                [LatestMatch] => 2016-05-24T02:15:00+00:00
                [IsCup] => false
            )
        [41] => SimpleXMLElement Object
            (
                [Id] => 56
                [Name] => EURO 2016
                [Country] => International
                [Historical_Data] => Partial
                [Fixtures] => Yes
                [Livescore] => Yes
                [NumberOfMatches] => 48
                [LatestMatch] => 2016-07-10T21:00:00+00:00
                [IsCup] => true
            )
    )
[AccountInformation] => Data requested at 7/17/2016 11:05:08 AM .

)
New record created successfully
Process finished with exit code 0

That’s because you run the insert query outside the foreach loop, so it only runs once and with whatever data was the last one in the object. Move it inside the loop before the closing } bracket, that should sort it.

Hi thank you for the replay i was busy few days, i fixed the last issu, but now the data from the xml for HomeGoalDetails cant be saved in to table.

The XML data

<XMLSOCCER.COM>
<Match>
<Id>96099</Id>
<FixtureMatch_Id>362047</FixtureMatch_Id>
<Date>2016-05-15T11:30:00+00:00</Date>
<Round>38</Round>
<Spectators>16020</Spectators>
<League>Scottish Premier League</League>
<HomeTeam>Hearts</HomeTeam>
<HomeTeam_Id>50</HomeTeam_Id>
<HomeCorners>4</HomeCorners>
<HomeGoals>2</HomeGoals>
<HalfTimeHomeGoals>2</HalfTimeHomeGoals>
<HomeShots>6</HomeShots>
<HomeShotsOnTarget>5</HomeShotsOnTarget>
<HomeFouls>19</HomeFouls>
<HomeGoalDetails>
20':Own Joe Shaughnessy;17': Arnaud Sutchuin Djoum;
</HomeGoalDetails>
<HomeLineupGoalkeeper>Jack Hamilton</HomeLineupGoalkeeper>
<HomeLineupDefense>
Callum Paterson; John Souttar; Alim Oezturk; Juwon Oshaniwa;
</HomeLineupDefense>
<HomeLineupMidfield>
Arnaud Sutchuin Djoum; Perry Kitchen; Prince Buaben; Lewis Moore;
</HomeLineupMidfield>
<HomeLineupForward>Abiola Dauda; Juan Delgado;</HomeLineupForward>
<HomeLineupSubstitutes>
Dario Zanatta; Neil Alexander; Sam Nicholson; Jordan McGhee; Gavin Reilly; Liam Smith; Miguel Pallardo;
</HomeLineupSubstitutes>
<HomeYellowCards>4</HomeYellowCards>
<HomeRedCards>1</HomeRedCards>
<HomeSubDetails>
68': in Gavin Reilly;68': out Juan Delgado;62': out Lewis Moore;62': in Sam Nicholson;53': out Juwon Oshaniwa;53': in Liam Smith;
</HomeSubDetails>
<AwaySubDetails>
83': in Steven MacLean;83': out Liam Gordon;68': in Greg Hurst;68': out Christopher Kane;
</AwaySubDetails>
<HomeTeamFormation>4-4-2</HomeTeamFormation>
<AwayTeam>St Johnstone</AwayTeam>
<AwayTeam_Id>46</AwayTeam_Id>
<AwayCorners>8</AwayCorners>
<AwayGoals>2</AwayGoals>
<HalfTimeAwayGoals>2</HalfTimeAwayGoals>
<AwayShots>26</AwayShots>
<AwayShotsOnTarget>7</AwayShotsOnTarget>
<AwayFouls>8</AwayFouls>
<AwayGoalDetails>12': Graham Cummins;9': penalty Liam Craig;</AwayGoalDetails>
<AwayLineupGoalkeeper>Alan Mannus</AwayLineupGoalkeeper>
<AwayLineupDefense>
Liam Gordon; Darnell Fisher; Joe Shaughnessy; Brian Easton;
</AwayLineupDefense>
<AwayLineupMidfield>
Liam Craig; David Wotherspoon; Daniel Swanson; Thomas Scobbie;
</AwayLineupMidfield>
<AwayLineupForward>Christopher Kane; Graham Cummins;</AwayLineupForward>
<AwayLineupSubstitutes>
Zander Clark; Eoghan McCawl; Michael Doyle; Steven Anderson; Greg Hurst; Steven MacLean; Scott Brown;
</AwayLineupSubstitutes>
<AwayYellowCards>4</AwayYellowCards>
<AwayRedCards>0</AwayRedCards>
<AwayTeamFormation>4-4-2</AwayTeamFormation>
<HomeTeamYellowCardDetails>
60': Juan Delgado;58': Arnaud Sutchuin Djoum;28': Juwon Oshaniwa;12': Alim Oezturk;8': John Souttar;
</HomeTeamYellowCardDetails>
<AwayTeamYellowCardDetails>
74': Thomas Scobbie;66': Liam Craig;59': Darnell Fisher;56': Christopher Kane;
</AwayTeamYellowCardDetails>
<HomeTeamRedCardDetails>57': Abiola Dauda;</HomeTeamRedCardDetails>
<AwayTeamRedCardDetails/>
<HomeLineupCoach>Robbie Neilson;</HomeLineupCoach>
<AwayLineupCoach>Tommy Wright;</AwayLineupCoach>
<HasBeenRescheduled>false</HasBeenRescheduled>
</Match>

The code

<?php
$file_name = basename(__FILE__,'.php');
include("conf.php");
include("XMLSoccer.php");

$years = 1;    ///<-------NUMBER OF YEARS TO GO BACK
$leagueretrive = 3;   ///<--------THE LEAGUE ID TO RETRIEVE DATA FOR

$date1 = date('y', strtotime("-$years years"));
$date2 = date("y");
//CHECKING IF TABLE EXIST IF NOT CREATE NEW
$table = $file_name;
$query = "SELECT ID FROM " . $table;
$resultat = mysqli_query($conn,$query);


if(empty($resultat)) {
    echo "<p>" . $table . " table does not exist</p>";
    $query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
        Id int NOT NULL PRIMARY KEY,
        FixtureMatch_Id varchar(255) NOT NULL,
        Date varchar(255) NOT NULL,
        HasBeenRescheduled varchar(255) NOT NULL,
        Round varchar(255) NOT NULL,
        Spectators varchar(255) NOT NULL,
        League varchar(255) NOT NULL,
        HomeTeam varchar(255) NOT NULL,
        HomeTeam_Id varchar(255) NOT NULL,
        HomeCorners varchar(255) NOT NULL,
        HomeGoals varchar(255) NOT NULL,
        HalfTimeHomeGoals varchar(255) NOT NULL,
        HomeShots varchar(255) NOT NULL,
        HomeShotsOnTarget varchar(255) NOT NULL,
        HomeFouls varchar(255) NOT NULL,
        HomeGoalDetails varchar(500) NOT NULL,
        HomeLineupGoalkeeper varchar(255) NOT NULL,
        HomeLineupDefense varchar(255) NOT NULL,
        HomeLineupMidfield varchar(255) NOT NULL,
        HomeLineupForward varchar(255) NOT NULL,
        HomeLineupSubstitutes varchar(255) NOT NULL,
        HomeYellowCards varchar(255) NOT NULL,
        HomeRedCards varchar(255) NOT NULL,
        HomeSubDetails varchar(255) NOT NULL,
        HomeTeamFormation varchar(255) NOT NULL,
        HomeTeamYellowCardDetails varchar(255) NOT NULL,
        HomeTeamRedCardDetails varchar(255) NOT NULL,
        HomeLineupCoach varchar(255) NOT NULL,
        AwayTeam varchar(255) NOT NULL,
        AwayTeam_Id varchar(255) NOT NULL,
        AwayCorners varchar(255) NOT NULL,
        AwayGoals varchar(255) NOT NULL,
        HalfTimeAwayGoals varchar(255) NOT NULL,
        AwayShots varchar(255) NOT NULL,
        AwayShotsOnTarget varchar(255) NOT NULL,
        AwayFouls varchar(255) NOT NULL,
        AwayGoalDetails varchar(255) NOT NULL,
        AwayLineupGoalkeeper varchar(255) NOT NULL,
        AwayLineupDefense varchar(255) NOT NULL,
        AwayLineupMidfield varchar(255) NOT NULL,
        AwayLineupForward varchar(255) NOT NULL,
        AwayLineupSubstitutes varchar(255) NOT NULL,
        AwayYellowCards varchar(255) NOT NULL,
        AwayRedCards varchar(255) NOT NULL,
        AwaySubDetails varchar(255) NOT NULL,
        AwayTeamFormation varchar(255) NOT NULL,
        AwayTeamYellowCardDetails varchar(255) NOT NULL,
        AwayTeamRedCardDetails varchar(255) NOT NULL,
        AwayLineupCoach varchar(255) NOT NULL
    )CHARACTER SET utf8 COLLATE utf8_general_ci");
}
else {
    echo "<p>" . $table . "table exists</p>";
} // else

/////GETING THE DATA FROM SERVICE

try {
    $soccer = new XMLSoccer($api_key);
    $soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
    $results = $soccer->GetHistoricMatchesByLeagueAndSeason(array("league"=>$leagueretrive,"seasonDateString"=>"$date1$date2"));
    print_r($results);
} catch (XMLSoccerException $e) {
    echo "XMLSoccerException: " . $e->getMessage();
}

foreach ($results->Match as $team) {
    $id = $team->Id;
    $fixtureid = $team->FixtureMatch_Id;
    $date = $team->Date;
    $hasBeenRescheduled = $team->HasBeenRescheduled;
    $round = $team->Round;
    $spectators = $team->Spectators;
    $league = $team->League;
    $hometeam = $team->HomeTeam;
    $hometeamid = $team->HomeTeam_Id;
    $homecorners = $team->HomeCorners;
    $homegoals = $team->HomeGoals;
    $halftimehomegoals = $team->HalfTimeHomeGoals;
    $homeshots = $team->HomeShots;
    $homeshotsontarget = $team->HomeShotsOnTarget;
    $homefouls = $team->HomeFouls;
    $homeGoalDetails = $team->HomeGoalDetails;
    $homeLineupGoalkeeper = $team->HomeLineupGoalkeeper;
    $homeLineupDefense = $team->HomeLineupDefense;
    $homeLineupMidfield = $team->HomeLineupMidfield;
    $homeLineupForward = $team->HomeLineupForward;
    $homeLineupSubstitutes = $team->HomeLineupSubstitutes;
    $homeYellowCards = $team->HomeYellowCards;
    $homeRedCards = $team->HomeRedCards;
    $homeSubDetails = $team->HomeSubDetails;
    $homeTeamFormation = $team->HomeTeamFormation;
    $homeTeamYellowCardDetails = $team->HomeTeamYellowCardDetails;
    $homeTeamRedCardDetails = $team->HomeTeamRedCardDetails;
    $homeLineupCoach = $team->HomeLineupCoach;
    $awayTeam = $team->AwayTeam;
    $awayTeam_Id = $team->AwayTeam_Id;
    $awayCorners = $team->AwayCorners;
    $awayGoals = $team->AwayGoals;
    $halfTimeAwayGoals = $team->HalfTimeAwayGoals;
    $awayShots = $team->AwayShots;
    $awayShotsOnTarget = $team->AwayShotsOnTarget;
    $awayFouls = $team->AwayFouls;
    $awayGoalDetails = $team->AwayGoalDetails;
    $awayLineupGoalkeeper = $team->AwayLineupGoalkeeper;
    $awayLineupDefense = $team->AwayLineupDefense;
    $awayLineupMidfield = $team->AwayLineupMidfield;
    $awayLineupForward = $team->AwayLineupForward;
    $awayLineupSubstitutes = $team->AwayLineupSubstitutes;
    $awayYellowCards = $team->AwayYellowCards;
    $awayRedCards = $team->AwayRedCards;
    $awaySubDetails = $team->AwaySubDetails;
    $awayTeamFormation = $team->AwayTeamFormation;
    $awayTeamYellowCardDetails = $team->AwayTeamYellowCardDetails;
    $awayTeamRedCardDetails = $team->AwayTeamRedCardDetails;
    $awayLineupCoach = $team->AwayLineupCoach;

///INSERTING DATA INTO THE TABLE
    $sql = "INSERT INTO $file_name (Id, FixtureMatch_Id, Date, HasBeenRescheduled, Round, Spectators, League, HomeTeam, HomeTeam_Id, HomeCorners, HomeGoals, HalfTimeHomeGoals, HomeShots, HomeShotsOnTarget,
 HomeFouls, HomeGoalDetails)
VALUES ('$id', '$fixtureid', '$date', '$hasBeenRescheduled', '$round', '$spectators', '$league', '$hometeam', '$hometeamid', '$homecorners', '$homegoals', '$halfTimeAwayGoals', '$homeshots', '$homeshotsontarget', 
'$homefouls', '$homeGoalDetails') 
 on duplicate key update Id='$id', FixtureMatch_Id='$fixtureid', Date='$date', HasBeenRescheduled='$hasBeenRescheduled', Round='$round', Spectators='$spectators', League='$league', HomeTeam='$hometeam', HomeTeam_Id='$hometeamid', 
HomeCorners='$homecorners', HomeGoals='$homegoals', HalfTimeHomeGoals='$halftimehomegoals', HomeShots='$homeshots', HomeShotsOnTarget='$homeshotsontarget', HomeFouls='$homefouls', HomeGoalDetails='$homeGoalDetails'";
if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
$conn->close();
?>

l try this to add

foreach ($results->HomeGoalDetails as $team) {
    $homeGoalDetails = $team->HomeGoalDetails;
}
$sql2 = "INSERT INTO $file_name (HomeGoalDetails)
VALUES ('$homeGoalDetails') 
 on duplicate key update HomeGoalDetails='$homeGoalDetails'";

It doesn’t work HELP!!!

Well, the biggest problem with your final bit of code:

foreach ($results->HomeGoalDetails as $team) {
    $homeGoalDetails = $team->HomeGoalDetails;
}
$sql2 = "INSERT INTO $file_name (HomeGoalDetails)
VALUES ('$homeGoalDetails') 
 on duplicate key update HomeGoalDetails='$homeGoalDetails'";

is that you build (and presumably run) the query outside of the loop, so it would only ever work on the final item in the object. I don’t know whether that’s what is happening - “It doesn’t work” doesn’t give us a lot to go on. Precisely how does it “not work”?

If you add echo $homeGoalDetails; into the loop, does it display the values that you would expect? If not, then that is more of a problem than the query being in the wrong place.

It seems to me that HomeGoalDetails will have a varying number of entries, depending on how many home goals are scored, so can your database layout deal with that properly? Presumably you have a separate table for that information, linked to the main table by team-id? The same would apply to other fields such as HomeSubDetails and AwaySubDetails, there isn’t going to be a good way to keep all those in the same table.

Those fields seem to also be XML objects within the main one, so you’ll need to extract the data correctly as you do for the main one. So, first crack that, then you can figure out where they should be stored.

Thank you for reply i tried with this. bdw Echo gives to me the exact data.

<?php
$file_name = basename(__FILE__,'.php');
include("conf.php");
include("XMLSoccer.php");

$years = 1;    ///<-------NUMBER OF YEARS TO GO BACK
$leagueretrive = 3;   ///<--------THE LEAGUE ID TO RETRIEVE DATA FOR

$date1 = date('y', strtotime("-$years years"));
$date2 = date("y");
//CHECKING IF TABLE EXIST IF NOT CREATE NEW
$table = $file_name;
$query = "SELECT ID FROM " . $table;
$resultat = mysqli_query($conn,$query);


if(empty($resultat)) {
    echo "<p>" . $table . " table does not exist</p>";
    $query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
        Id int NOT NULL PRIMARY KEY,
        HomeGoalDetails varchar(800) NOT NULL,
    )CHARACTER SET utf8 COLLATE utf8_general_ci");
}
else {
    echo "<p>" . $table . "table exists</p>";
} // else

/////GETING THE DATA FROM SERVICE

try {
    $soccer = new XMLSoccer($api_key);
    $soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
    $results = $soccer->GetHistoricMatchesByLeagueAndSeason(array("league"=>$leagueretrive,"seasonDateString"=>"$date1$date2"));
    print_r($results);
} catch (XMLSoccerException $e) {
    echo "XMLSoccerException: " . $e->getMessage();
}
foreach ($results->Match as $team) {
    $id = $team->Id;
    $homeGoalDetails = $team->HomeGoalDetails;

///INSERTING DATA INTO THE TABLE
    $sql = "INSERT INTO $file_name (HomeGoalDetails)
VALUES ('$homeGoalDetails')
on duplicate key update HomeGoalDetails='$homeGoalDetails'";
}
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
$conn->close();
?>

I get this in response

Error: INSERT INTO testing2 (HomeGoalDetails)
VALUES (‘35’: Stefan Johansen;4’: penalty Leigh Griffiths;‘)
on duplicate key update HomeGoalDetails=‘35’: Stefan Johansen;4’: penalty Leigh Griffiths;‘
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ‘: Stefan Johansen;4’: penalty Leigh Griffiths;’)
on duplicate key update HomeGo’ at line 2
Process finished with exit code 0

I would say the quotes are causing the trouble there - you have a string value that contains single quotes, but don’t seem to be doing anything to escape them so as to not confuse MySQL.

I tried to replace quotes but still i get an error

<?php
$file_name = basename(__FILE__,'.php');
include("conf.php");
include("XMLSoccer.php");

$years = 1;    ///<-------NUMBER OF YEARS TO GO BACK
$leagueretrive = 3;   ///<--------THE LEAGUE ID TO RETRIEVE DATA FOR

$date1 = date('y', strtotime("-$years years"));
$date2 = date("y");
//CHECKING IF TABLE EXIST IF NOT CREATE NEW
$table = $file_name;
$query = "SELECT ID FROM " . $table;
$resultat = mysqli_query($conn,$query);


if(empty($resultat)) {
    echo "<p>" . $table . " table does not exist</p>";
    $query = mysqli_query($conn,"CREATE TABLE IF NOT EXISTS $file_name (
        Id int NOT NULL PRIMARY KEY,
        HGoaldetails varchar(800) NOT NULL,
    )CHARACTER SET utf8 COLLATE utf8_general_ci");
}
else {
    echo "<p>" . $table . "table exists</p>";
} // else

/////GETING THE DATA FROM SERVICE

try {
    $soccer = new XMLSoccer($api_key);
    $soccer->setServiceUrl("http://www.xmlsoccer.com/FootballDataDemo.asmx");
    $results = $soccer->GetHistoricMatchesByLeagueAndSeason(array("league"=>$leagueretrive,"seasonDateString"=>"$date1$date2"));
    print_r($results);
} catch (XMLSoccerException $e) {
    echo "XMLSoccerException: " . $e->getMessage();
}
foreach ($results->Match as $team) {
    $id = $team->Id;
    $homeGoalDetails = $team->HomeGoalDetails;
    $remove[] = "'";
    $remove[] = '+';
    $remove[] = ";"; // just as another example

    $replace[] = "";
    $replace[] = "";
    $replace[] = '|'; // just as another example

    $string = str_replace($remove, $replace, $homeGoalDetails);
///INSERTING DATA INTO THE TABLE
    $sql = "INSERT INTO $file_name (HGoaldetails)
VALUES ('$string')
on duplicate key update HGoaldetails='$string'";
}
    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
$conn->close();
?>

result

Error: INSERT INTO testing2 (HGoaldetails)
VALUES (‘35: Stefan Johansen|4: penalty Leigh Griffiths|’)
on duplicate key update HGoaldetails=‘35: Stefan Johansen|4: penalty Leigh Griffiths|’
Table ‘database.testing2’ doesn’t exist
Process finished with exit code 0

Did you read the error message where it says your table does not exist? Does it exist or not?

Thank you very much for the help you give to me, I add mysqli_real_escape_string and it works, all the best it was a pleasure.

Excellent, glad it’s now working. I still think you’d be better redesigning the database and splitting these multiple-part fields into their constituents, as it will make it much easier to use them in searches later on. But if it works for now that’s great.

For your inserts it will be more efficient if you use a single prepare statement and then just bind/execute each row to be inserted. That will also do away with the need to escape the data as the code will then be completely separate.

I TRIED THIS CODE BUT I GET AN ERROR

<?php
include("conf.php");
$query = mysqli_query($conn,"SELECT HomeTeam, COUNT(HomeGoals) FROM GetHistoricMatchesByLeagueAndSeason GROUP BY HomeTeam ORDER BY HomeTeam ASC");
//Create the `mysqli_connection` and assign to `$conn` variable.
$result = mysqli_query($conn, $query);
if($result->num_rows>0)
{
    while($row = mysqli_fetch_assoc($result))
    {
        echo "<br>Team=" . $row['HomeTeam'];
        echo "<br>SUM=" . $row['HomeGoals'];
    }
}
?>

PHP Warning: mysqli_query() expects parameter 2 to be string, object given in C:\TEST.php on line 5

PHP Notice: Trying to get property of non-object in C:\TEST.php on line 6
Warning: mysqli_query() expects parameter 2 to be string, object given in C:\TEST.php on line 5

Notice: Trying to get property of non-object in C:\TEST.php on line 6

Process finished with exit code 0