简体   繁体   English

为什么我不能从该php文件中获取XML输出?

[英]Why am I not getting XML output from this php file?

I'm supposed to be getting XML output from the following php file that is accessing information from a database. 我应该从下面的php文件中获取XML输出,该文件正在访问数据库中的信息。

Here's the php file: 这是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>';

 ?>

Here's a link to the XML that I should be getting: 这是我应该获得的XML链接:

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

Here's a link to the site that I'm not getting output from: 这是我没有从中获得输出的网站链接:

http://thehobbit2movie.com/phpsqlajax_genxml.php http://thehobbit2movie.com/phpsqlajax_genxml.php

I think that it is returning fine, it just might not be visible in your browser unless you choose "view source" on the blank looking page. 我认为它返回的很好,除非您在空白页面中选择“查看源代码”,否则它可能在您的浏览器中不可见。

Try to modifying the code just after the header() call so it looks like: 尝试在header()调用之后立即修改代码,如下所示:

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

Hum, couple of comment to improve your code 哼,几句注释可以改善您的代码

1- parseToXML could be replaced by the native htmlspecialchars function. 1- parseToXML可以由本机htmlspecialchars函数代替。

http://php.net/manual/en/function.htmlspecialchars.php http://php.net/manual/en/function.htmlspecialchars.php

2- SELECT * FROM markers WHERE 1 2-选择*从标记WHERE 1

No need for the WHERE 1, you just need SELECT * FROM markers. 不需要WHERE 1,只需要SELECT * FROM标记。 WHERE is not needed when you don't filter 不过滤时不需要在哪里

OK, to answer your question, try removing the @ in from of mysql_fetch_assoc. OK,要回答您的问题,请尝试从mysql_fetch_assoc中删除@ in。 You may see an error appearing. 您可能会看到一个错误出现。 That is probably what is preventing your script from executing correctly. 这可能是导致脚本无法正确执行的原因。

Hope that help 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM