Charles Yeung
Charles Yeung

Reputation: 38805

PHP SimpleXML produce some unknown encoding

When I execute the code below,

mysql_connect("localhost", "user", "pass") or die("Could not connect: " . mysql_error());
mysql_select_db("database");
mysql_query ("set character_set_results='utf8'");
$result = mysql_query("SELECT xxx");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  $str = $row[0];
  echo $str;
}

I got the message below if I check the sources(Chinese words, UTF-8 encoding as below),

LS23A300BS(23”LED顯示器)/ LS22A300BS(21.5”LED顯示器)

But, when I added the code to convert the SQL result to XML file as below,

$dom = new domDocument;
$dom->formatOutput = true;

$root = $dom->appendChild($dom->createElement( "items" ));
$sxe = simplexml_import_dom($dom);

mysql_connect("localhost", "user", "pass") or die("Could not connect: " . mysql_error());
mysql_select_db("database");
mysql_query ("set character_set_results='utf8'");
$result = mysql_query("SELECT xxx");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  $item = $sxe->addChild("item");
  $str = $row[0];  
  $item->addChild("desc", $str);
}

echo $sxe->asXML();

mysql_free_result($result);

I got the message below if I check the sources (unknown encoding as below),

LS23A300BS(23”LED顯示器)/ LS22A300BS(21.5”LED顯示器)

I want to ask,
What is the type of the unknown encoding?
How can I change this unknown encoding back to UTF-8?

Thanks

Upvotes: 0

Views: 141

Answers (1)

Francis Avila
Francis Avila

Reputation: 31621

Two things to try:

  1. Use mysql_query('SET NAMES utf8'); instead of character_set_results. This sets a few more key MySQL settings to utf8.
  2. You don't declare the encoding of your DOMDocument. Try using this as your constructor: new DOMDocument('1.0', 'UTF-8')

This is an aside: You don't need DOMDocument at all here. Try $sxe = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><items/>');

Upvotes: 1

Related Questions