简体   繁体   中英

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

I am trying to upload the xml to mysqldatabase using php code. i am struck with the following error.. Notice: Trying to get property of non-object in D:\\Program Files\\xampp\\htdocs\\xml\\xml_extract.php on line 12 Warning: Invalid argument supplied for foreach() in D:\\Program Files\\xampp\\htdocs\\xml\\xml_extract.php on line 12 getting this error what object should i add in foreach My xml looks like

<?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>

this is my php code

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

} ?>

change this line

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

with this one

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

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