尝试使用 php 将数据从 xml 导入到 phpmyadmin 中的 mysql 数据库


Trying to importing the data from xml using php to mysql database in phpmyadmin

我正在尝试使用php代码将xml上传到mysqldatabase。 我遇到了以下错误。注意:尝试获取第 12 行 D:''Program Files''xampp''htdocs''xml''xml_extract.php 中非对象的属性 警告:在第 12 行的 D:''Program Files''xampp''htdocs''xml''xml_extract.php 中为 foreach() 提供的参数无效收到此错误 我应该为每个对象添加什么对象我的 xml 看起来像

<?xml version="1.0" encoding="utf-8">
    <db>    
    <tb name="data">
        <column name="ID">Rd 1098</column>
        <column name="name">Reynolds</column>
        <column name="header">Its me</column>
        <column name="note">Hi folks</column>
        <column name="roll">3678</column>
        <column name="quantity">45</column>
        <column name="numeric">1</column>
        <column name="price">100</column>
    </tb>
    <tb name="data">
        <column name="ID">RD 8734</column>
        <column name="name">Cello </column>
        <column name="header">Long lasting</column>
        <column name="note">Best in quality</column>
        <column name="roll">24</column> 
        <column name="quantity">87</column>
        <column name="numeric">2</column>
        <column name="price">143</column>
    </tb>
    </db>

这是我的PHP代码

<?php
$url = "D:''ta.xml";
$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","root","");
mysql_select_db("new_xml_extract",$con) or die(mysql_error());
foreach($xml -> tb as $row){
$ID= $row->ID;
$name=$row->name;
$header=$row->header;
$note = $row->note;
$roll=$row->roll;
$quantity=$row->quantity;
$numeric = $row->numeric;
$price=$row->price;
$sql = "insert into   'content'('ID','name','header','note','roll','quantity,'numeric','price')" ."values ('$ID','$name','$header','$note','$roll','$quantity','$numeric','$price')";
$result = mysql_query($sql);
if(!$result){
    echo 'my sql error';
}
else{
    echo'success';
}

}?>

更改此行

foreach($xml -> tb as $row){

有了这个

foreach($xml->xpath('//tb') as $row){