我有以下與我在PHP編寫的XML文件中的代碼,但是當我運行這段代碼,我得到以下錯誤獲取有關XMLELEMENT警告,而在PHP編寫XML文件
SimpleXMLElement::asXML(): string is not in UTF-8
代碼生成XML如下所示
<?php
//Create Database connection
$mysqli = new mysqli('localhost', 'user', 'pass', 'dbnam');
if(mysqli_connect_errno()) {
echo "Connection Failed: " . mysqli_connect_errno();
}
$query='select Siteurl from tablename order by colname2 desc ';
$result = mysqli_query($mysqli,$query);
//Create SimpleXMLElement object
$xml = new SimpleXMLElement('<urls/>');
//Add each column value a node of the XML object
while($row = mysqli_fetch_array($result)) {
$mydata = $xml->addChild('url');
$mydata->loc=$row['Siteurl'];
}
mysqli_close($mysqli);
//Create the XML file
$fp = fopen("sitemaps/sitemap2.xml","wb");
//Write the XML nodes
fwrite($fp,$xml->asXML());
//Close the database connection
fclose($fp);
?>
我怎樣才能糾正錯誤guys.Please幫我
我檢查,對MySQL連接其返回UTF8,但爲什麼即時得到這個錯誤,那麼 – user3843585
@ user3843585:不是連接最終是原因,而是實際的字符串。你仍然需要自己驗證,設置正確的mysql連接字符集是一個先決條件(而不是這樣做的一個常見錯誤,但它只是其中一個可能出錯的東西)。 – hakre
k.k。用函數utf8_encode函數修改錯誤 – user3843585