简体   繁体   中英

How do I import XML data to MySQL? (Linux hosting with cPanel from Godaddy)

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';
    }

?>

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."')";

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