【发布时间】: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