我正在尝试获取一个简单的 PHP 服务来为 iPhone 应用程序生成 XML,从 SQL 数据库中获取。该数据库有多种文本源和字符,其中一些似乎与脚本无关并产生以下编码错误:
第 1 行第 6232 列的错误:编码错误
php如下:
<?php
//database configuration
$config['mysql_host'] = "XXX.XXX.XXX.XXX";
$config['mysql_user'] = "XXXX";
$config['mysql_pass'] = "XXXX";
$config['db_name'] = "XXXX";
$config['table_name'] = "articles";
//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
$root_element = $config['table_name']."s";
$xml .= "<$root_element>";
$sql = "SELECT * FROM ".$config['table_name'];
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)>0)
{
while($result_array = mysql_fetch_assoc($result))
{
$xml .= "<".$config['table_name'].">";
//loop
foreach($result_array as $key => $value)
{
$xml .= "<$key>";
$xml .= "<![CDATA[$value]]>";
//and close the element
$xml .= "</$key>";
}
$xml.="</".$config['table_name'].">";
}
}
$xml .= "</$root_element>";
//send xml
header ("Content-Type:text/xml");
echo $xml;
?>
有些文章会导致整个页面失败,有些则不会。任何帮助将不胜感激。