PHP 输出不以 xml 格式打印

标签 php mysql xml

我有以下代码。

$stmt = $conn->prepare('SELECT sponsor_path FROM sponsors WHERE conference_id = ?');
$stmt->execute(array($cid));

$sponsorsImg = $stmt->fetchall (PDO::FETCH_OBJ);

$xml_output = "<?xml version=\"1.0\" ?>";
$xml_output .= "<sponsors>";
foreach ($sponsorsImg as $spImg){
    $xml_output .= "<sponsor>\n";
    $xml_output .= "<image>".$spImg->sponsor_path."</image>\n";
    $xml_output .= "</sponsor>\n";
}
$xml_output .= "</sponsors>";

echo $xml_output;

我没有以 xml 格式打印结果,而是以纯文本格式仅获取 $spImg->sponsor_path 的内容。有任何想法吗?

最佳答案

大多数浏览器将呈现看起来像 XML 的标记。因此可能会出现您想要的 XML 实际生成但未在浏览器上正确显示的情况。

尝试以下操作:

输出前设置内容类型:

header('Content-Type: text/xml');
echo $xml_output;

如果输出看起来相同,则右键单击页面并选择查看源代码。您应该会在那里看到预期的 XML 标记。

关于PHP 输出不以 xml 格式打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26546958/

相关文章:

php - 使用 php 填充 javascript 数组

mysql - 如何从mysql存储过程或函数中获取Concat逗号分隔值?

xml - 在 SQL Server 2008 R2 中压缩 XML 列

Java XPath : Can't find an element

xml - XPath设置多个值

php - 点击更新状态

php - Laravel将可变成功消息从存储库传递回 Controller

php - 将日期时间列值批量转换并更新为 UNIX 时间戳?

mysql - 尝试创建一个 Access 2013 查询,该查询选择上一年 9 月 1 日到当年 8 月 31 日之间的日期

mysql - 数据库中最近的自由坐标