Эта страница содержит следующие ошибки: ошибка в строке 2 в столбце 6: XML объявление допускается только в начале документа - PullRequest
0 голосов
/ 23 апреля 2020

Не могу найти решение, пожалуйста, помогите. ниже код. Заранее спасибо

    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();  
?>

и вот дБ. 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.

help мне нужно это очень быстро для поступления в университет на следующей неделе

1 Ответ

0 голосов
/ 24 апреля 2020

Удалите все пустые места до <? и после ?> в ваших файлах.

...