doucu9677 2015-06-23 17:03
浏览 65
已采纳

PHP,在写入Xml文件时使用explode函数错误

I'm trying to write some locations into an xml file in order to use them later it was working until I decide to use explode it gives me then an error which is:"error on line 2 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error."Here is the code:

<?php 
$conn=mysqli_connect("localhost", "thecode007", "007","offers") or die(mysql_error());

$query = "SELECT * FROM Branches";
$result = mysqli_query($conn,$query) or die(mysql_error());

 $doc = new DomDocument('1.0');
$node = $doc->createElement("markers");
$parnode = $doc->appendChild($node);

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

while($row = mysqli_fetch_array($result))
{
$node = $doc->createElement("marker");
$newnode = $parnode->appendChild($node);
$loc=explode(",",$row);
$newnode->setAttribute("location", $loc[0]);
}

print $doc->saveXML();

?>

</div>
  • 写回答

1条回答 默认 最新

  • douxian9060 2015-06-23 17:11
    关注

    The problem is with explode:

    $loc=explode(",",$row);
    

    $row is an array already. Explode is used to split strings into arrays. This is difficult without seeing what the data returned for the query is, but assuming you want the entire first column of your result to be $loc, then just remove the explode() call and replace with:

    $loc = $row[0];
    

    Note, this is super-bad. You should replace the * in your select with the names of actual columns. In this form your program is very brittle because a change to the first column would change what info is being stored in $loc.

    ps. and yes, remove the ?> at the end of the file, but that was not the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分