Tim Johnstone
Tim Johnstone

Reputation: 363

Outputting XML via PHP and SQL

Can anyone help me in by telling me how can I output my XML result via PHP? I have an sql database and written the function in PHP to parse the XML and I have debugged the page in Firefox (using the NET tab) which is bringing up a the correct response corresponding to my SQL statement however, I can't actually see the data, its just a blank page.

Here is the php file to write the XML:

<?php
  include("classes/database_connection.php");

    function parseToXML($htmlStr) 
       { 
        $xmlStr=str_replace('<','&lt;',$htmlStr); 
        $xmlStr=str_replace('>','&gt;',$xmlStr); 
        $xmlStr=str_replace('"','&quot;',$xmlStr); 
        $xmlStr=str_replace("'",'&#39;',$xmlStr); 
        $xmlStr=str_replace("&",'&amp;',$xmlStr); 
        return $xmlStr; 
        } 

        // Opens a connection to a MySQL server
        $connection=mysql_connect ('www.numyspace.co.uk', '*********', '************');
         if (!$connection) {
           die('Not connected : ' . mysql_error());
           }


         $db_selected = mysql_select_db('********', $connection);
           if (!$db_selected) {
            die ('Can\'t use db : ' . mysql_error());
            }


        // Select all the rows in the ticket table
       $query = "SELECT * FROM ticket";
       $result = mysql_query($query);


       header("Content-type: text/xml");

       // Start XML file, echo parent node
       echo '<ticket>';

       while ($row = @mysql_fetch_assoc($result)){
       // ADD TO XML DOCUMENT NODE
       echo '<tickets ';
       echo 'ticketID="' . parseToXML($row['ticketID']) . '" ';
       echo 'locationID="' . parseToXML($row['locationID']) . '" ';
       echo 'venue="' . parseToXML($row['venue']) . '" ';
       echo 'tPrice="' . parseToXML($row['tPrice']) . '" ';
       echo 'date="' . parseToXML($row['date']) . '" ';
       echo 'availability="' . parseToXML($row['availability']) . '" ';
       echo 'time="' . parseToXML($row['time']) . '" ';
       echo 'lat="' . $row['lat'] . '" ';
       echo 'lng="' . $row['lng'] . '" ';
       echo '/>';
     }

   // End XML file
   echo '</ticket>';

?>

Thank you and any help is much appreciated.

EDIT: The page's markup:

 <ticket><tickets ticketID="1" locationID="1" venue="The Cluny" tPrice="15" date="2012-04-17" availability="200" time="20:00:00" lat="54.978252" lng="-1.617780" /><tickets ticketID="2" locationID="1"..../></ticket>

Upvotes: 1

Views: 2466

Answers (1)

Josh
Josh

Reputation: 8191

Ensure you have the XML declaration at the top of your XML.

echo "<?xml version='1.0' ?>";

Upvotes: 1

Related Questions