title: Exporting MySQL data to xml using PHP - HTML encoding and general error BRAIN MELT
body: <p>Anyone who can answer my question deserves a BIG FAT GOLD MEDAL OF AWESOMENESS!</p>
  
  <p>I'm trying to get  the contents of a MySQL table into a nice, easy XML format. I'm running a bit of PHP which works great and I can see XML (Good times). However, in the MySQL table there are a couple of fields which are populated with non-encoded HTML table code. I'm wrapping each field value that I get inside CDATA tags, I've made sure that the xml tags are being closed, but I'm wondering if I'm missing something because it's erroring and I can't see why (Bad times). It looked fine to me, so I tried to open this in Excel (as that's how the client will see it) it claimed that the start tag of "package" was matched with an end tag of "long_description".</p>
  
  <p><a href="http://www.shavesgreensafaris.com/display.php" rel="nofollow">http://www.shavesgreensafaris.com/display.php</a> is the page that I'm working on so you can see the data there.</p>
  
  <p>This is the code I'm using...</p>
  
  <pre><code>        $xml          = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;";
          $root_element = "packages";
          $xml         .= "&lt;$root_element&gt;";
  
  
          if(mysql_num_rows($result)&gt;0)
          {
             while($result_array = mysql_fetch_assoc($result))
             {
                $xml .= "&lt;package&gt;";
  
                //loop through each key,value pair in row
                foreach($result_array as $key =&gt; $value)
                {
                   //$key holds the table column name
                   $xml .= "&lt;$key&gt;";
  
                   //embed the SQL data in a CDATA element to avoid XML entity issues
                   $xml .= "&lt;![CDATA[$value]]&gt;"; 
  
                   //and close the element
                   $xml .= "&lt;/$key&gt;";
                }
  
                $xml.="&lt;/package&gt;";
             }
          }
  
  //close the root element
  $xml .= "&lt;/$root_element&gt;";
  
  //send the xml header to the browser
  header ("Content-Type:text/xml"); 
  
  //output the XML data
  echo $xml;
  </code></pre>
  
  <p>What ON EARTH am I doing wrong?!</p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>OK, so there appears to be something that removes one "&lt;" from a long_description tag around line 310 - you can search in the view source if you want to see it as ">long_description>" - something really weird has happened and the tag isn't properly formed. I wasn't entirely sure HOW this could happen as the code I'm using definitely puts opening and closing angle brackets on all $keys. It only happens once, but it seems to stuff everything else up. </p>
  
  <p>Anyone know why this might be occurring?</p>
  
  <p>Any help HUGELY appreciated, and thanks in advance!</p>
  
  <p>Gem</p>