我有以下代码。
$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/