Page 1 of 1

pushing xml values into a mysql database record

Posted: Fri Jul 17, 2009 10:44 am
by loislane
As a newbie, I'm having trouble figuring out how to open a mysql dB and keep it open while I populate a record, field by field.

I'm reading out values from an xml file. Now I want to push those values into a mysql database. But my code isn't working. Please, how do you iterate through database fields without naming them specifically?

Here's my code:

Code: Select all

<?php
   
if (! ($xmlparser = xml_parser_create()) )
{ 
   die ("Cannot create parser");
}
 
function start_tag($parser, $name, $attribs) {
   echo "Current tag : ".$name."<br />";
   if (is_array($attribs)) {
      echo "Attributes : <br />";
      $dbc = @mysqli_connect ('xxx', 'xxx', 'xxx', 'xxx') OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
      while(list($key,$val) = each($attribs)) {
         echo "Attribute ".$key." has value ".$val."<br />";
         
         $query = "INSERT INTO markers VALUES ('" . $val . "')";
         mysqli_query($dbc, $query);
         
 
       }
       mysqli_close($dbc);
    }
}
 
function end_tag($parser, $name) {
   echo "Reached ending tag ".$name."<br /><br />";
}
 
xml_set_element_handler($xmlparser, "start_tag", "end_tag");
 
function tag_contents($parser, $data) {
   echo "Contents : ".$data."<br />";
}
 
xml_set_character_data_handler($xmlparser, "tag_contents");
 
$filename = "myxmlfile.xml";
 
if (!($fp = fopen($filename, "r"))) { die("cannot open ".$filename); }
 
while ($data = fread($fp, 4096)){
   $data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
   if (!xml_parse($xmlparser, $data, feof($fp))) {
      $reason = xml_error_string(xml_get_error_code($xmlparser));
      $reason .= xml_get_current_line_number($xmlparser);
      die($reason);
   }
}
xml_parser_free($xmlparser);
 
?>

Re: pushing xml values into a mysql database record

Posted: Thu Jul 23, 2009 11:33 am
by akuji36
Take a look at the following video tutorial link:

http://www.phpvideotutorials.com/free

and see the old videos link.

Then watch the last 2 videos about mysql and php.
They'll help you out a lot.