pushing xml values into a mysql database record
Posted: Fri Jul 17, 2009 10:44 am
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:
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);
?>