dongliehuan3925 2017-05-09 09:39
浏览 44

使用PHP从XML插入数据库

I am trying to Insert data from XML to MySql.

So far i have written this: ?php

$servername = "localhost";
$username = "root";
$password = "rootuser";
$dbname = "my_data";

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


$string = file_get_contents('rss.xml');
$xml = new SimpleXMLElement($string);

echo 'All Values of a Group: -';
foreach($xml->system->system as $item){
    echo $item->MY_Name.' - ';
    echo $item->Version.' - ';
    echo '<br/>';
    mysqli_query($conn, "INSERT INTO customer_data_table (My_Name, Version ) VALUES (". $item->My_Name.", '". $item->Version."')" );
}
?

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • duanqian8867 2017-05-09 09:46
    关注

    In PHP use xml parser to get the values from the XML. Here is the code

    $xml = '<system>
    <Name>new</Name>
    <Month>Feb</Month>
    </system>';
    echo "<pre>";
    print_r(new SimpleXMLElement($xml));
    

    Here you can get all the values from XML tag and you can insert into Database.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画