php - 为什么我没有从这个 php 文件中得到 XML 输出?

标签 php mysql xml database

我应该从以下从数据库访问信息的 php 文件中获取 XML 输出。

这是 php 文件:

 <?php
 require("phpsqlajax_dbinfo.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 ($server, $username, $password);
 if (!$connection) {
   die('Not connected : ' . mysql_error());
 }

 // Set the active MySQL database
 $db_selected = mysql_select_db($database, $connection);
 if (!$db_selected) {
   die ('Can\'t use db : ' . mysql_error());
 }

 // Select all the rows in the markers table
 $query = "SELECT * FROM markers WHERE 1";
 $result = mysql_query($query);
 if (!$result) {
   die('Invalid query: ' . mysql_error());
 }

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

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

 // Iterate through the rows, printing XML nodes for each
 while ($row = @mysql_fetch_assoc($result)){
   // ADD TO XML DOCUMENT NODE
   echo '<marker ';
   echo 'name="' . parseToXML($row['name']) . '" ';
   echo 'address="' . parseToXML($row['address']) . '" ';
   echo 'lat="' . $row['lat'] . '" ';
   echo 'lng="' . $row['lng'] . '" ';
   echo 'type="' . $row['type'] . '" ';
   echo '/>';
 }

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

 ?>

这是我应该获得的 XML 的链接:

http://code.google.com/apis/earth/articles/phpsqlearth.html

这是我没有从中获取输出的网站链接:

http://thehobbit2movie.com/phpsqlajax_genxml.php

最佳答案

我认为它返回正常,除非您在空白页面上选择“查看源代码”,否则它可能在您的浏览器中不可见。

尝试在 header() 调用之后修改代码,使其看起来像:

// Start XML file, echo parent node
echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
echo '<markers>';

关于php - 为什么我没有从这个 php 文件中得到 XML 输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5760901/

相关文章:

android - Android 中的布局响应问题

java - 如何解析具有多个字符实体的XML?

PHP For 循环与 Mysqli 的问题

php - laravel 4 在哪里存储选择字段值?数据库还是数组?

php - 在 html 页面上显示 php 结果

php - 如何根据mysql数据库记录对angularjs中的记录进行分组

xml - XSL : Replacing certain characters with xml tags

php - 如何在 HTML 中禁用嵌入式媒体播放器中的暂停和搜索栏

MySQL查询返回的行数比预期多,我如何只返回每组中一列的最高值?

mysql - 使用 "join"和 "group by"避免子查询