我尝试通过以 utf8 编码提取 sql 表值来找到解决方案,但没有运气。下面的代码导出了一个名为“test.xml”但没有编码的正确 XML 文件。有什么建议么?
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "admin";
$dbname = "My_DB";
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname);
$sql = "SELECT product_id, model, image FROM product";
$q = mysql_query($sql)or die(mysql_error());
$xml = "<products>";
while($r = mysql_fetch_array($q)){
$xml .= "<product>";
$xml .= "<id>";
$xml .= "<![CDATA[".$r["product_id"]."]]>";
$xml .= "</id>";
$xml .= "<name><![CDATA[" . $r["model"] . "]]></name>";
$xml .= "<image>".$r['image']."</image>";
$xml .= "</product>";
}
$xml .= "</products>";
$sxe = new SimpleXMLElement($xml);
$sxe->asXML("test.xml");
?>