我试图使用php创建一个xml文件。每次我运行代码时,页面都会在屏幕上从某一点以文本形式显示代码。我的代码如下:
if(!$dbconnet = mysql_connect('I took out the details')){
echo"connection failed to the host.";
exit;
}
if (!mysql_select_db('siamsati_db')){
echo"Cannot connect to the database.";
exit;
}
$table_id = 'events';
$query ="SELECT * FROM $table_id";
$dbresult = mysql_query($query, $dbconnect);
$doc = new DomDocument('1.0');
$root = $doc->createElement('root');
$root = $doc->appendChild($root);
while($row = mysql_fetch_assoc($dbresult)){
$ooc = $doc->createElement($table_id);
$occ = $root->appendChild($occ);
foreach ( $row as $fieldname => $fieldvalue){
$child = $doc->createElement($fieldname);
$child = $occ->appendchild($child);
$value = $doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
}
}
$xml_string = $doc->saveXML();
echo $xml_string;
?>
并且显示的页面显示:
createElement('root'); $root =
$doc->appendChild($root); while($row =
mysql_fetch_assoc($dbresult)){ $ooc =
$doc->createElement($table_id); $occ =
$root->appendChild($occ); foreach (
$row as $fieldname => $fieldvalue){
$child =
$doc->createElement($fieldname);
$child = $occ->appendchild($child);
$value =
$doc->createTextNode($fieldvalue);
$value = $child->appendChild($value);
} } $xml_string = $doc->saveXML();
echo $xml_string; ?>
香港专业教育学院错过了什么。我首先检查了所有引文,认为它们都是正确的,但它们似乎都是正确的。关于我做错什么的任何建议都值得赞赏?
如果执行包含以下内容的页面会发生什么:
读取$root = $doc->createElement(root);的行中>之前是否有?? 这将使PHP解释器切换到HTML模式,并转储随后的所有内容,以提供您发布的输出。 至少我无法想象您使用->作为结束标记编译了PHP。 那太恶心了。
里克你的话很可能是问题,我想不出任何其他可能性。
上面的代码与我所拥有的代码完全相同,只是我在上面的代码的末尾省略了结束标记。 $ root = $ doc-> createElement(root)行中的>之前。
告诉你var_dump($ row); 看看有什么输出?
使用PHP和MySQL生成XML文件-kvcodes.com/2017/03/generate-xml-file-using-php-mysql
将内容类型设置为XML,以便浏览器将其识别为XML。
header("content-type: application/xml; charset=ISO-8859-15" );
在您的代码中将其更改为:
// Set the content type to be XML, so that the browser will recognise it as XML.
header("content-type: application/xml; charset=ISO-8859-15" );
//"Create" the document.
$doc = new DOMDocument("1.0","ISO-8859-15" );
+++我想你可以做这样的事情
foreach ( $row as $fieldname => $fieldvalue){
?>
=fieldname; ?>
=$fieldvalue; ?>
}
?>
您可以print_r($ row)并使用它更新您的问题吗
在您在此处发布的代码中,缺少初始标记...