gpt4 book ai didi

php - 从 PHP 生成 XML 时解析属性时出错

转载 作者:行者123 更新时间:2023-11-28 23:11:28 25 4
gpt4 key购买 nike

我试图通过使用 MySQL 中的数据在我的网站中实现谷歌地图,但最终遇到了这个错误

This page contains the following errors:

error on line 1 at column 18: error parsing attribute name

Below is a rendering of the page up to the first error.

下面是提供的代码:https://developers.google.com/maps/documentation/javascript/mysql-to-maps

我可以知道导致错误的原因吗?

<?php
require("connect.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 ('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");

// 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 'id="' . $ind . '" ';
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>';

?>

最佳答案

看起来例子是错误的。

echo 'id="' . $ind . '" ';

应该是

echo 'id="' . $row['id'] . '" ';

(如果失败,请尝试从 mysql_fetch_assoc 调用中删除错误抑制以查看数据库是否抛出错误。即删除 @)

无论哪种情况,发生的情况都是这样。

您正在输出一个 XML 文档,正在呈现该文档 - 但您得到了

error on line 1 at column 18: error parsing attribute name

因为 XML 在 name 属性处格式错误 - 这会告诉我您几乎肯定在 XML 输出中有一个 php 错误。

例如

<markers>
<marker id="Notice: Undefined variable: "id" in ~\whatever\kml.php on line blah blah blah
" name="foo" address="bar" ... />}

您可以通过 curl 或类似方式调用文件,或在大多数浏览器中通过“查看源代码”来确认这一点。

最后,如果不是这种情况,请尝试在打开的 php 标记之后立即在文件顶部打开错误报告,例如。

<?php
ini_set('display_errors', 1);
error_reporting(~0);

然后再次检查脚本的实际输出。

关于php - 从 PHP 生成 XML 时解析属性时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45783194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com