0

iam trying to make an php file to import xml to sql database, but i have this error-:

Warning: Invalid argument supplied for foreach() in /home/morrisse/public_html/xml_to_database.php on line 18

My code:

        $url ="http://www.example/example.xml";
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $URL);  //GET THE URL CONTENDS

    $data = curl_exec ($ch);  //execute the curl request

    curl_close($ch);

    $xml = simplexml_load_string($data);
    foreach ($xml -> track as $row) {
                $title = $row -> name;
                $artist = $row -> artist;
                $duration = $row -> duration;
    // performing sql query

    $sql = "INSERT INTO 'test_xml' ('title', 'artist', 'duration')"
                . "VALUES ('$title', '$artist', '$duration')";

    $result = mysql_query($sql);
    if (!result) {
                    echo 'MYSQL ERROR';
                }   else {
                echo 'SUCESSO';
                }
                }

my xml:

      <lfm status="ok">
    <track>
    <id>602366984</id>
    <name>Barbarism Begins At Home (BBC TV Tube performance)</name>
    <mbid/>
    <url>http://www.last.fm/music/The+Smiths/_/Barbarism+Begins+At+Home+(BBC+TV+Tube+performance)</url>
    <duration>384000</duration>
    <streamable fulltrack="0">0</streamable>
    <listeners>10</listeners>
    <playcount>24</playcount>
    <artist>
    <name>The Smiths</name>
    <mbid>40f5d9e4-2de7-4f2d-ad41-e31a9a9fea27</mbid>
    <url>http://www.last.fm/music/The+Smiths</url>
    </artist>
    <toptags>
    </toptags>
    </track>
    </lfm>

help please?

The full code:

<?php

$url ="http://www.morrisseyradio.com/shoutcastinfo/cache/track.xml";
$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);  //GET THE URL CONTENDS

$data = curl_exec ($ch);  //execute the curl request

curl_close($ch);
$con=mysql_connect("localhost", "", ""); //connect to database
mysql_select_db("morrisse_forum", $con) 
if (!$con) {
    die('Could not connect: ' . mysql_error());
}; // select database


libxml_use_internal_errors(true);
$xml = simplexml_load_string($data);


if ($xml === false) {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
        echo "XML Error at line $error->line: $error->message\n";
    }
}
 else {
foreach ($xml -> track as $row) {
            $title = $row -> name;
            $artist = $row -> artist;
            $duration = $row -> duration;
// performing sql query

$sql = "INSERT INTO 'test_xml' ('title', 'artist', 'duration')"
            . "VALUES ('$title', '$artist', '$duration')";

$result = mysql_query($sql);
if (!result) {
                echo 'MYSQL ERROR';
            }   else {
            echo 'SUCESSO';
            }
            }
            }

?>
  • 2
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jan 09 '15 at 23:06
  • Thank you Quentin, will do that, i am learning. – Handsome_devil Jan 09 '15 at 23:09
  • But for this example what is the error please? – Handsome_devil Jan 09 '15 at 23:17
  • Please edit your question to show how you created the variable called `$xml` in your program. Your error message is telling you that variable isn't set up right. – O. Jones Jan 09 '15 at 23:32
  • the error message is correct. it's good you have had it, the error was there for real. – hakre Apr 03 '15 at 10:10

2 Answers2

1

For anyone the answer is: First thing: in the line:

curl_setopt($ch, CURLOPT_URL, $URL);

the "$URL" must be Lower

Second: In this line:

$sql = "INSERT INTO 'test_xml' ('title', 'artist', 'duration')"

the "'" must be "`"

  • Right. The single quotes within the sql need to be backticks (same key as ~ on a US keyboard). Also, `artist` is a compound element in this XML. You might want `$row->artist->name` – O. Jones Jan 10 '15 at 13:12
0

It's pretty clear that your call to simplexml_load_string() failed because the xml was not well formed. When that happens you get back false rather than a usable xml element.

Here's what I suggest you do about this. Add some error-tracking code, as follows.

libxml_use_internal_errors(true);
$xml = simplexml_load_string($data);

if ($xml === false) {
    $errors = libxml_get_errors();
    foreach ($errors as $error) {
        echo "XML Error at line $error->line: $error->message\n";
    }
}
 else {
     foreach ( $xml->track as $row ) {  /* your code continues here  ... */

There's clearly something wrong with the xml you're getting back from that server you're using with curl. This code will help you figure out what is wrong.

O. Jones
  • 103,626
  • 17
  • 118
  • 172