【问题标题】:This page contains the following errors: error on line 2 at column 6: XML declaration allowed only at the start of the document此页面包含以下错误:第 2 行第 6 列的错误:XML 声明只允许在文档的开头
【发布时间】:2021-04-14 19:03:54
【问题描述】:

我无法找到解决方案,请帮助。下面是代码。提前致谢

    require_once "db.php";
    $sQuery = "SELECT price ,year,carID FROM cars ";

    $result = $conn->query($sQuery);
    $dom = new DOMDocument( "1.0");

    $root = $dom->createElement('cars');

    while($row = $result->fetch()){
        $car_node = $dom->createElement('car');

        $car_id = $row['carID'];
        $car_node->setAttribute( "id", $car_id); 

        $car_year = $row['year'];
        $car_year_node = $dom->createElement('year' ,$car_year);
        $car_node->appendChild($car_year_node);

        $root->appendChild($car_node);
}   $dom->appendChild($root); 

    header('Content-Type: application/xml');
    $dom->formatOutput = true;
    print $dom->saveXML();  
?>

这里是 db.php 连接文件

<?php

$server_name = "localhost"; $user_name = "root"; $password = ""; $db_name = "databse";
try
{
 $conn = new PDO("mysql:host=$server_name;dbname=$db_name", $user_name, $password);
}
catch(PDOException $e)
 {
    echo $sql . "<br>" . $e->getMessage();
 }
?>

同时我不断收到同样的错误

This page contains the following errors:
error on line 2 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

请帮我尽快完成下周到期的大学作业

【问题讨论】:

  • 您的输出结果如何?错误消息说,在 xml 声明之前有内容。在输出之前可能会抛出 PHP 错误、警告或通知。很高兴知道错误的堆栈跟踪是什么样的。
  • 通常最好避免说您急需解决方案。有些人会避免卷入你急于赶上最后期限的情况。
  • 您是否尝试过联系您的教授?至少我对帮助我解决家庭作业问题非常敏感,并且已经看到了许多常见错误。

标签: php xml xml-parsing pear


【解决方案1】:

删除文件中&lt;? 之前和?&gt; 之后的所有空格。

【讨论】:

  • 详细说明在您的文件中,这意味着根页面以及来自该根页面的整个requirerequire_once 层次结构。
猜你喜欢
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多