【问题标题】:PHP not returning XML after iterating through SQL database遍历 SQL 数据库后 PHP 不返回 XML
【发布时间】:2013-07-11 18:52:08
【问题描述】:

我对 PHP 完全陌生。我只需要一个简短的项目来将 CSV 导入 MYSQL 数据库,然后通过 Google Maps API 查询数据库。如果可以的话,我会很感激婴儿步骤/资源。

我正在尝试连接到包含名称、经度和纬度(以及其他内容)的数据库,在标记表上执行 SELECT * 查询,并遍历结果。对于表中的每一行(每个位置),我需要创建一个新的 XML 节点,其中行属性作为 XML 属性,并将其附加到父节点。然后将 XML 转储到屏幕上。

我正在使用此代码:

<?php  

require("phpsqlajax_dbinfo.php"); 

// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node); 

// Opens a connection to a MySQL server

$connection=mysql_connect (localhost, $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"); 

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetchAssoc($result)){  
  // ADD TO XML DOCUMENT NODE  
  $node = $dom->createElement("marker");  
  $newnode = $parnode->appendChild($node);   

  $newnode->setAttribute("name", $row['name']);
  $newnode->setAttribute("city", $row['city']);
  $newnode->setAttribute("country", $row['country']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("skill_1", $row['skill_1']);
  $newnode->setAttribute("skill_2", $row['skill_2']);
  $newnode->setAttribute("skill_3", $row['skill_3']);
  $newnode->setAttribute("interest_1", $row['interest_1']);
  $newnode->setAttribute("interest_2", $row['interest_2']);
  $newnode->setAttribute("interest_3", $row['interest_3']);

} 

echo $dom->saveXML();

?>

我已经尝试进行基本调试,我可以确定 1) 程序正确连接到数据库; 2) 没有出现错误日志; 3) 错误出现在注释“ // 选择标记表中的所有行”之后立即开始。

XML 数据应该会显示出来,但它没有,而且我已经把头发拉出来两个小时了。有什么想法吗?

==编辑== 我没有意识到“@”会消除错误消息。删除它后,此错误日志会显示在我的网站上。有什么想法吗?

[11-Jul-2013 13:08:00] PHP Warning:  Cannot modify header information - headers already sent by (output started at /home5/dreamio2/public_html/admin/phpsqlajax_dbinfo.php:7) in /home5/dreamio2/public_html/admin/phpsqlajax_genxml.php on line 31
[11-Jul-2013 13:08:00] PHP Warning:  DOMElement::setAttribute() [<a href='domelement.setattribute'>domelement.setattribute</a>]: string is not in UTF-8 in /home5/dreamio2/public_html/admin/phpsqlajax_genxml.php on line 40
[11-Jul-2013 13:08:00] PHP Warning:  DOMElement::setAttribute() [<a href='domelement.setattribute'>domelement.setattribute</a>]: string is not in UTF-8 in /home5/dreamio2/public_html/admin/phpsqlajax_genxml.php on line 40
[11-Jul-2013 13:08:00] PHP Warning:  DOMDocument::saveXML() [<a href='domdocument.savexml'>domdocument.savexml</a>]: output conversion failed due to conv error, bytes 0xE9 0x73 0x20 0x41 in /home5/dreamio2/public_html/admin/phpsqlajax_genxml.php on line 54

==解决方案==

经过与 Manoj 的长时间讨论,我发现我有两个错误:1)我需要对我的所有变量进行 UTF_8 编码(请参阅他的回答); 2) 包含我的用户名和密码的 php 文件在 close 语句后有 TRAILING WHITESPACE。谢谢,Manoj!

【问题讨论】:

  • 如果您评论整个while,您的输出是什么?
  • 无输出,出现此错误:[11-Jul-2013 13:14:09] PHP 警告:无法修改标头信息 - 标头已由(输出开始于 /home5/dreamio2/public_html/ admin/phpsqlajax_dbinfo.php:7) 在 /home5/dreamio2/public_html/admin/phpsqlajax_genxml.php 第 31 行
  • header() 函数在调用前如果有输出则不起作用。输出是您所需脚本中的警告。
  • 我可以看到 2 个选项。修复这些警告或关闭它们(不推荐,也许它根本不起作用)在 require() 函数之前设置 error_reporting(0)ini_set('display_errors', 0)

标签: php xml google-maps google-maps-api-3 encoding


【解决方案1】:

你打错了function mysql_fetchAssoc 正确的是mysql_fetch_assoc

请不要使用@ 使错误输出静音,因为错误输出有助于解决问题。

当您面临 utf8 问题时,请使用 utf8_encode,如下所示:

$newnode->setAttribute("name", utf8_encode($row['name']));
$newnode->setAttribute("city", utf8_encode($row['city']));
$newnode->setAttribute("country", utf8_encode($row['country']));
$newnode->setAttribute("lat", utf8_encode($row['lat']));
$newnode->setAttribute("lng", utf8_encode($row['lng']));
$newnode->setAttribute("skill_1", utf8_encode($row['skill_1']));
$newnode->setAttribute("skill_2", utf8_encode($row['skill_2']));
$newnode->setAttribute("skill_3", utf8_encode($row['skill_3']));
$newnode->setAttribute("interest_1", utf8_encode($row['interest_1']));
$newnode->setAttribute("interest_2", utf8_encode($row['interest_2']));
$newnode->setAttribute("interest_3", utf8_encode($row['interest_3']));

【讨论】:

  • 我编辑了代码,仍然没有骰子。 “mysql_fetchAssoc”直接来自谷歌帮助页面,也可以在这个例子中看到:stackoverflow.com/questions/8800659/…,所以我怀疑这是问题所在。
  • 好的,但是mysql_fetchAssoc 不是函数,它的错字php.net/manual/en/function.mysql-fetch-assoc.php
  • 我明白了。我修改了,还是不行。我相信这个问题可能与 UTF 编码有关?见这里:stackoverflow.com/questions/8127225/…。我只是不明白如何使用它
  • 评论此行$newnode-&gt;setAttribute("name", $row['name']); 或替换为$newnode-&gt;setAttribute("name", utf8_encode($row['name'])); 并尝试
  • 我已更新 utf8 问题的答案,并查看页面源 (ctrl + u),因为 XML 可能不可见,因为 Content-Type header 未设置为 XML
猜你喜欢
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多