Salve,
Per esercizio sto facendo in php un sistema che ma i dati di una tabella MySql in un file XML esterno.
Il mio problema che xml mi restituisce un errore di parsing sui caratteri speciali e accentati.
Vorrei sapere se esiste un modo per ovviare a questo.
Il codice:
Per esercizio sto facendo in php un sistema che ma i dati di una tabella MySql in un file XML esterno.
Il mio problema che xml mi restituisce un errore di parsing sui caratteri speciali e accentati.
Vorrei sapere se esiste un modo per ovviare a questo.
Il codice:
PHP:
<?php
//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "root";
$config['mysql_pass'] = "";
$config['db_name'] = "demo";
$config['table_name'] = "countries";
//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']; //countries
$xml .= "<$root_element>";
//select all items in table
$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 .= "<country>";
//loop through each key,value pair in row
foreach($result_array as $key => $value)
{
//$key holds the table column name
$xml .= "<$key>";
//embed the SQL data in a CDATA element to avoid XML entity issues
$xml .= "<![CDATA[$value]]>";
//and close the element
$xml .= "</$key>";
}
$xml.="</country>";
}
}
//close the root element
$xml .= "</$root_element>";
//send the xml header to the browser
header ("Content-Type:text/xml");
//output the XML data
file_put_contents("countries.xml", $xml);
echo $xml;
?>