Jump to content

Problem with XML MYSQL PHP Import Script?


innitboy

Recommended Posts

This script turns all the xml tags into arrays and inputs the relevant data into the field names in my table

I cant get it to loop over each result (or top level tag),

It only shows and imports the last result in the resultset

Would appreciate any help?

[code]<?php
    include 'library/config.php';
    include 'library/opendb.php';
$file = "my-xml.xml";

$feed = array();
$key = "";
$info = "";


function startElement($xml_parser,  $attrs ) {
  global $feed; 
  }

function endElement($xml_parser, $name) {
  global $feed,  $info;
  $key = $name;
  $feed[$key] = $info;
  $info = ""; }

function charData($xml_parser, $data ) {
  global $info;
 
 
  $info .= $data; }
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "charData" );
$fp = fopen($file, "r");
while ($data = fread($fp, 8192))
!xml_parse($xml_parser, $data, feof($fp));
xml_parser_free($xml_parser);

$sql= "INSERT INTO section ( ";
$j=0;
$i=count($feed);

foreach( $feed as $assoc_index => $value )
  {
  $j++;
  $sql.= strtolower($assoc_index);
  if($i>$j) $sql.= " , ";
  if($i<=$j) {$sql.= " ) VALUES ('";}
  }
$h=0;
foreach( $feed as $assoc_index => $value )
  {
  $h++;
  $sql.= utf8_decode(trim(addslashes($value)));
  if($i-1>$h) $sql.= "', '";
  if($i<=$h) $sql.= "','');";
  }
  $query=trim($sql);
  echo $value;
if (mysql_query($sql)){
echo "success in table creation.";
} else {
echo "no table created.";
}
echo mysql_error();
  echo $sql;
?> [/code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.