此页面包含以下错误:第2行第6列错误:仅在文档开头允许XML声明

问题描述 投票:0回答:1

我无法找到解决方案,请帮助。下面是代码。在此先感谢

    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.

[帮助我,下周要完成大学任务,请非常快速地使用此]

php xml xml-parsing pear
1个回答
0
投票

删除文件中<?之前和?>之后的所有空白。

© www.soinside.com 2019 - 2024. All rights reserved.