通过PHP和SQL输出XML [英] Outputting XML via PHP and SQL

查看:90
本文介绍了通过PHP和SQL输出XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我如何通过PHP输出XML结果吗?我有一个sql数据库,并用PHP编写了函数以解析XML,并且我已经在Firefox中调试了页面(使用NET选项卡),该页面显示了与我的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.

这是用于编写XML的php文件:

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.

页面的标记:

 <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>

推荐答案

确保XML声明位于XML的顶部.

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

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

这篇关于通过PHP和SQL输出XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆