0

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

1 Answer 1

1

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/>');

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Thanks, solved the problem by changing new domDocument to new DOMDocument('1.0', 'UTF-8').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.