此页面包含以下错误:第 2 行第 6 列错误:XML 声明仅允许在文档开头
This page contains the following errors: error on line 2 at column 6: XML declaration allowed only at the start of the document
我找不到解决办法,请帮忙。下面是代码。提前致谢
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.
帮助我,下周要交的大学作业需要这个非常快
删除文件中 <?
之前和 ?>
之后的所有空白 space。
我找不到解决办法,请帮忙。下面是代码。提前致谢
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.
帮助我,下周要交的大学作业需要这个非常快
删除文件中 <?
之前和 ?>
之后的所有空白 space。