Jump to content

Recommended Posts

I need help with this old script I found. Parse Error on line 101. And Line 101 is ?>  :shrug:

 

<?php
  $file = "music.xml";
  $to_print = array("Name", "Artist", "Album", "Track ID", "Year", "Play Count", "Track Number", "Track Count", "Genre", "Rating", "Date Added");
  $db_host = "localhost";
  $db_name = "music_library";
  $db_table = "table";
  $db_username = "root";
  $db_password = "";
  
  function db_connect()
  {
      global $db_host, $db_name, $db_table, $db_username, $db_password;
      mysql_connect($db_host, $db_username, $db_password) or die("<p style='font-color:red'>Cannot connect to mySQL server</p>");
      mysql_select_db($db_name) or die("<p style='font-color:red'>Cannot connect to mySQL database</p>");
  }
  function alter_print_arr(&$input, $key)
  {
      $input = str_replace(' ', '_', strtolower($input));
  }
  array_walk($to_print, 'alter_print_arr');
  function array_to_table($array)
  {
      global $db_table, $to_print;
      db_connect();
      mysql_query("DELETE FROM $db_table") or die("Could not remove old records.");
      mysql_query("OPTIMIZE TABLE $db_table");
      foreach ($array as $elem_key => $element) {
          if (isset($element[track_id])) {
              $sql = "";
              foreach ($element as $k => $v) {
                  if (in_array($k, $to_print)) {
                      $sql .= "$k='" . mysql_real_escape_string(str_replace('=amp=', '&', $v)) . "', ";
                  }
              }
              $sql = rtrim(ltrim($sql, "track_id='$element[track_id]', "), ", ");
              $sql1 = "INSERT INTO $db_table (track_id) VALUES ('$element[track_id]');";
              $sql2 = "UPDATE $db_table SET $sql WHERE track_id=$element[track_id];";
              mysql_query($sql1) or die(mysql_error());
              // echo"$sql1<br />$sql2<br /><br />"; // For debugging. Uncomment with caution! 
              mysql_query($sql2) or die(mysql_error());
          }
      }
      echo "Done! ";
      // print_r($array); // For debugging. Uncomment with caution! } 
      $xml_parser = "";
      //will hold each song in a 2-d array 
      $songs = array();
      //counter, number of 'dict' elements encountered $current_key=""; 
      $number_dicts = 0;
      //key for each element in second dimension of array $current_element=""; //stores xml element name 
      //value for second dimension array elements 
      $current_data = "";
      //boolean used to help let us know if we're done with the song list 
      $end_of_songs = false;
      
      function start_element($parser, $name, $attribs)
      {
          global $current_element, $number_dicts;
          if ($name == "DICT") {
              $number_dicts++;
          }
          if ($number_dicts > 2) {
              $current_element = $name;
          }
      }
      function end_element($parser, $name)
      {
          global $songs, $current_element, $current_data, $number_dicts, $array_key, $end_of_songs;
          if ($end_of_songs) {
              return;
          }
          if ($current_element == "KEY") {
              $array_key = str_replace(' ', '_', strtolower($current_data));
          } else {
              $songs[$number_dicts][$array_key] = $current_data;
          }
      }
      function character_data($parser, $data)
      {
          global $number_dicts, $current_data, $end_of_songs;
          if ($data == "Playlists") {
              $end_of_songs = true;
          }
          $current_data = trim($data);
      }
      $xml_parser = xml_parser_create();
      xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
      xml_set_element_handler($xml_parser, "start_element", "end_element");
      xml_set_character_data_handler($xml_parser, "character_data");
      if (!($fp = @fopen($file, "r"))) {
          return false;
      }
      while ($data = fread($fp, 4096)) {
          // xml_parser jumps over ampersands. Decode any entities then replace any ampersands. // Reverse this when building SQL statement. 
          if (!xml_parse($xml_parser, str_replace('&', '=amp=', html_entity_decode($data)), feof($fp))) {
              die(sprintf("XML error: %s at line %d ", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
          }
      }
      xml_parser_free($xml_parser);
      array_to_table($songs);
?>

Link to comment
https://forums.phpfreaks.com/topic/210304-xml-parser-script-error/
Share on other sites

function array_to_table wasn't closed

 

try this *untested*

<?php
$file = "music.xml";
$to_print = array("Name", "Artist", "Album", "Track ID", "Year", "Play Count", "Track Number", "Track Count", "Genre", "Rating", "Date Added");
$db_host = "localhost";
$db_name = "music_library";
$db_table = "table";
$db_username = "root";
$db_password = "";

function db_connect() {
    global $db_host, $db_name, $db_table, $db_username, $db_password;
    mysql_connect($db_host, $db_username, $db_password) or die("<p style='font-color:red'>Cannot connect to mySQL server</p>");
    mysql_select_db($db_name) or die("<p style='font-color:red'>Cannot connect to mySQL database</p>");
}
function alter_print_arr(&$input, $key) {
    $input = str_replace(' ', '_', strtolower($input));
}

function array_to_table($array) {
    global $db_table, $to_print;
    db_connect();
    mysql_query("DELETE FROM $db_table") or die("Could not remove old records.");
    mysql_query("OPTIMIZE TABLE $db_table");
    foreach ($array as $elem_key => $element) {
        if (isset($element[track_id])) {
            $sql = "";
            foreach ($element as $k => $v) {
                if (in_array($k, $to_print)) {
                    $sql .= "$k='" . mysql_real_escape_string(str_replace('=amp=', '&', $v)) . "', ";
                }
            }
            $sql = rtrim(ltrim($sql, "track_id='$element[track_id]', "), ", ");
            $sql1 = "INSERT INTO $db_table (track_id) VALUES ('$element[track_id]');";
            $sql2 = "UPDATE $db_table SET $sql WHERE track_id=$element[track_id];";
            mysql_query($sql1) or die(mysql_error());
            // echo"$sql1<br />$sql2<br /><br />"; // For debugging. Uncomment with caution! 
            mysql_query($sql2) or die(mysql_error());
        }
    }
}

function start_element($parser, $name, $attribs) {
    global $current_element, $number_dicts;
    if ($name == "DICT") {
        $number_dicts++;
    }
    if ($number_dicts > 2) {
        $current_element = $name;
    }
}
function end_element($parser, $name) {
    global $songs, $current_element, $current_data, $number_dicts, $array_key, $end_of_songs;
    if ($end_of_songs) {
        return;
    }
    if ($current_element == "KEY") {
        $array_key = str_replace(' ', '_', strtolower($current_data));
    } else {
        $songs[$number_dicts][$array_key] = $current_data;
    }
}
function character_data($parser, $data) {
    global $number_dicts, $current_data, $end_of_songs;
    if ($data == "Playlists") {
        $end_of_songs = true;
    }
    $current_data = trim($data);
}
array_walk($to_print, 'alter_print_arr');

// print_r($array); // For debugging. Uncomment with caution! } 
$xml_parser = "";
//will hold each song in a 2-d array 
$songs = array();
//counter, number of 'dict' elements encountered $current_key=""; 
$number_dicts = 0;
//key for each element in second dimension of array $current_element=""; //stores xml element name 
//value for second dimension array elements 
$current_data = "";
//boolean used to help let us know if we're done with the song list 
$end_of_songs = false;

$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($xml_parser, "start_element", "end_element");
xml_set_character_data_handler($xml_parser, "character_data");
if (!($fp = @fopen($file, "r"))) {
    return false;
}
while ($data = fread($fp, 4096)) {
    // xml_parser jumps over ampersands. Decode any entities then replace any ampersands. // Reverse this when building SQL statement. 
    if (!xml_parse($xml_parser, str_replace('&', '=amp=', html_entity_decode($data)), feof($fp))) {
        die(sprintf("XML error: %s at line %d ", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);
array_to_table($songs);

echo "Done! ";
?>

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.