php - 此页面包含以下错误:第 2 行第 6 列的错误:XML 声明只允许在文档的开头
问题描述
我无法找到解决方案,请帮助。下面是代码。提前致谢
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.
帮我,请很快就需要这个来完成下周到期的大学作业
解决方案
<?
删除文件中前后的所有空格?>
。
推荐阅读
- javascript - React bootstrap table 2以编程方式选择过滤器
- angular - How to pass function as data to another module component in angular?
- amazon-web-services - 无法创建集群。启动 Redshift 集群时 VPC 显示为灰色
- azure - 如何在 Azure 数据工厂接收器中添加“文本限定符”
- django - Django Autocomplete Light create_field 不工作
- assembly - 从有符号/无符号字符到无符号/有符号整数类型转换的 IA32 汇编代码
- javascript - OnClick() 事件在我的代码中的动态按钮的 JavaScript 中不起作用
- python - 无法将发布数据发送到 django 中的 api
- associative-array - 系统 Verilog 关联数组
- javascript - 在 Django 项目中使用时,Javascript 查询选择器不读取类和 ID