MySQL Forums
Forum List  »  PHP

XML to MYSQL, script check
Posted by: White Horse
Date: April 12, 2010 08:37AM

Hello community,
I want to import this xml code to mysql - my script runs without errors but there is nothing in database.
So if there is some nice soul to check where I made mistake :shrug: i would be very thankful. I m very new to PHP (programming in general :/)

I only need these information extracted:
<description> and
<krit measurand="KRIT" unit="%">0</krit><traffic_out> under <traffic_out>

XML CODE:

 <?xml version="1.0" encoding="UTF-8" ?> 
- <!-- # wanstats 
  --> 
- <cacti>
- <report>
- <settings>
  <title>Wan_links</title> 
  <owner>Administrator (admin)</owner> 
  <template>wan_statistic</template> 
  <start>2010-04-03</start> 
  <end>2010-04-09</end> 
  <last_run>2010-04-10 22:34:05</last_run> 
  </settings>
- <variables>
- <variable>
  <id>3</id> 
  <name>over90percent</name> 
  <description>Your description</description> 
  <value>10</value> 
  <min_value>10</min_value> 
  <max_value>100</max_value> 
  </variable>
  </variables>
- <measurands>
- <measurand>
  <abbreviation>CAPA</abbreviation> 
  <description>kapacitet</description> 
  </measurand>
- <measurand>
  <abbreviation>CRIT</abbreviation> 
  <description>critical - time (%) more than 10% usage</description> 
  </measurand>
  </measurands>
- <data_items>
- <item>
  <description>MIAMI - wan546 - Gi1/0/8</description> 
  <subhead /> 
  <start_day>Monday</start_day> 
  <end_day>Sunday</end_day> 
  <start_time>00:00:00</start_time> 
  <end_time>24:00:00</end_time> 
  <time_zone>GMT</time_zone> 
- <results>
- <traffic_in>
  <krit measurand="KRIT" unit="%">0</krit> 
  </traffic_in>
- <traffic_out>
  <krit measurand="KRIT" unit="%">0</krit> 
  </traffic_out>
- <overall>
  <kapa measurand="KAPA" unit="Bits/s">5.76E+8</kapa> 
  </overall>
  </results>
  </item>
- <item>
  <description>LONDON[2] -  wan134_Gi4/1/0.10</description> 
  <subhead /> 
  <start_day>Monday</start_day> 
  <end_day>Sunday</end_day> 
  <start_time>00:00:00</start_time> 
  <end_time>24:00:00</end_time> 
  <time_zone>GMT</time_zone> 
- <results>
- <traffic_in>
  <krit measurand="KRIT" unit="%">0</krit> 
  </traffic_in>
- <traffic_out>
  <krit measurand="KRIT" unit="%">0</krit> 
  </traffic_out>
- <overall>
  <kapa measurand="KAPA" unit="Bits/s">32000000</kapa> 
  </overall>
  </results>
  </item>
  </data_items>
  </report>
  </cacti>

PHP CODE;

 
<?php
//mysql connection
$con2 = mysql_connect("localhost","cactiuser","cactiuser");
if (!$con2)
  {
  die('Could not connect: ' . mysql_error());
  }

$selectdb = mysql_select_db("reports", $con2);
if (!$selectdb)
  {
 die('Database not used: ; ' . mysql_error());
  }

//simplexml load xml file
   $data_items = simplexml_load_file('/home/hanszimmer/temp/99/wanstats.xml');

//loop through parsed xmlfeed and print output

foreach ($data_items->item as $item) {
              printf("Description: %s\n", $item->description);
                     foreach ($item->results as $results) {
                     printf("Traffic_out: %s\n", $results->traffic_out);
                     
                   

//insert into databse
                               mysql_query("INSERT INTO report_percent (city_id, time_over_treshold) VALUES (\"$item->description\",\"$results->traffic_out\")")
                               or die(mysql_error());
//show updated records
            printf ("Records inserted: %d\n", mysql_affected_rows());
            }
        }
//close connection
 mysql_close($con2);

?>

Options: ReplyQuote


Subject
Written By
Posted
XML to MYSQL, script check
April 12, 2010 08:37AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.