0

I am trying to parse XML using PHP DOM and then insert this data in MySQL tables, i am using the following code for this:

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("testrtap11.xml");
mysql_select_db("zeeshan_database1", $con);

$x=$xmlDoc->getElementsByTagName('RECORD');
$z=$xmlDoc->getElementsByTagName('TITLE');
$w=$xmlDoc->getElementsByTagName('PRIMARY_AUTHOR');
$y=$xmlDoc->getElementsByTagName('JOURNAL_CONFERENCE');

for($i=0; $i<=$x->length-1; $i++)
{
    $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES('$z->item($i)->nodeValue','$w->item($i)->nodeValue','$y->item($i)->nodeValue')";

    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    echo "1 record added";
}

mysql_close($con)
?>

The data being entered is not correct. i even tried storing my xml parse values in a variable and then using that variable to insert data, even that does not works. I used the variable like this:

for($i=0; $i<=$x->length-1; $i++)
{
    $zz=$z->item($i)->nodeValue);
    $ww=$w->item($i)->nodeValue);
    $yy=$y->item($i)->nodeValue);
    $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES('$zz','$ww','$yy')";

    if (!mysql_query($sql,$con))
    {
        die('Error: ' . mysql_error());
    }
    echo "1 record added";
}

My xml looks like this:

<RTAP>
  <RECORD>
     <TITLE>               </TITLE>
     <PRIMARY_AUTHOR>      </PRIMARY_AUTHOR>
     <JOURNAL_CONFERENCE>  </JOURNAL_CONFERENCE>
  </RECORD>
</RTAP>

Kindly help me, what should I do in this

Best Zeeshan

2
  • Can you please include the data that you were expecting to be stored and the data that was actually stored? Commented Jun 18, 2009 at 16:29
  • Have you tried debugging or printing values first? Commented Jun 18, 2009 at 16:30

2 Answers 2

2

Try something like this:

foreach ($xmlDoc->RECORD as $record)
{
    $sql = 'INSERT INTO Persons (FirstName, LastName, Age) VALUES('
        . '"' . mysql_escape_string($record->TITLE->nodeValue) . '", '
        . '"' . mysql_escape_string($record->PRIMARY_AUTHOR->nodeValue) . '", '
        . '"' . mysql_escape_string($record->JOURNAL_CONFERENCE->nodeValue) . '")'

    if (!mysql_query($sql,$con))
        die('Error: ' . mysql_error());

    echo "1 record added";
}

You don't actually say what's wrong with the data that's inserted...

Sign up to request clarification or add additional context in comments.

4 Comments

I tried using the select command: $result = mysql_query("SELECT * FROM Persons"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } And this is the output i got: (0)->nodeValue (0)->nodeValue (1)->nodeValue (1)->nodeValue (2)->nodeValue (2)->nodeValue (3)->nodeValue (3)->nodeValue
Greg what is the $record that you have mentioned in your code? is it like $z->item($i)->nodeValue that i am using in mine code. But the thing is your statement is not working for some reason-- i cant not figure out..
Hey greg thanks, your code did work out after just a few changes in it. thanks alot. Zeeshan
Oops, I'd made a complete mess of the quotes
0

I tried using the select command on my original code that i mentioned above: $result = mysql_query("SELECT * FROM Persons"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "
"; }

And this is the output i got: (0)->nodeValue (0)->nodeValue (1)->nodeValue (1)->nodeValue (2)->nodeValue (2)->nodeValue (3)->nodeValue (3)->nodeValue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.