Reputation: 51
I am attempting to import XML data into a MySQL database but I don't know how to make this work. I am using Linux hosting with cPanel from Godaddy with php 5.4. This script is from a tutorial, but even after filling out the necessary MySQL login information, it results in a blank page and no information is being added to the database table. What am I doing wrong?
<?php
$url ="XML.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec ($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
$con=mysql_connect("localhost","dbusername","dbpassword");
mysql_select_db("dbname", $con) or die(mysql_error());
foreach ($xml -> item as $row) {
$title = $row -> title;
$description = $row -> description;
$categories = $row -> categories;
$sql = "INSERT INTO 'test_xml' ('title', 'description', 'categories')"
. "VALUES ('$title', '$description', '$categories')";
$result = mysql_query($sql);
if (!$result) {
echo 'MySQL ERROR';
} else {
echo ' SUCCES';
}
?>
Upvotes: 3
Views: 217
Reputation: 8111
To fix the issue, Change your statement as below
Replace
$sql = "INSERT INTO 'test_xml' ('title', 'description', 'categories')"
. "VALUES ('$title', '$description', '$categories')";
with
$sql = "INSERT INTO 'test_xml' ('title', 'description', 'categories')"
. "VALUES ('".$title."', '".$description."', '".$categories."')";
Upvotes: 1