-1

My php file create XML using data from mysql database, I have some special characters in my database, that aren't showing up properly.s

PHP Code:

http://sandbox.onlinephpfunctions.com/code/8faec4b8da5f2edc76e23ec4a40ac6e35e03f2bc

SQL Table Structure, Data & Proof:

Link: https://dl-web.dropbox.com/get/Stack.png?_subject_uid=303112722&w=AADui4zBLmvS_ng879Kh5lPdveOE6dDIWC_obpzEZpUNRQ

Please help me fixing this, I have gone through stackoverflow and tried several things, no one worked.

Thanks,

1 Answers1

0

XMLWriter is a standard XML API for result lists - just like a database result. It works in a linear way, keeping the memory usage low.

The XMLWriter takes care of the escaping as needed. Make sure that you have set the encoding of your database connection to utf-8.

$records = [
  [
    'questionid' => '1',
    'item' => 'one'
  ],
  [
    'questionid' => '2',
    'item' => 'two'
  ]
];

header('Content-type: text/xml; charset: utf-8');
$out = new XMLWriter();
$out->openURI('php://output');
$out->setIndent(true);
$out->startDocument('1.0', 'UTF-8');
$out->startElement('quiz');

while (list($key, $record) = each($records)) {
  $out->startElement('question');
  $out->writeAttribute('id', $record['questionid']);
  $out->writeElement('item', $record['item']);
  $out->endElement();
}

$out->endElement();

Output:

<?xml version="1.0" encoding="UTF-8"?>
<quiz>
 <question id="1">
  <item>one</item>
 </question>
 <question id="2">
  <item>two</item>
 </question>
</quiz>
ThW
  • 19,120
  • 3
  • 22
  • 44
  • Hi thanks for answer, It is working when I supply character directly using 'quotes', but when I am retrieving data from database and display, its showing ? mark. THanks, – Ammar Brohi Feb 02 '15 at 20:04
  • Have you checked the database connection encoding? http://www.php.net/manual/en/mysqli.character-set-name.php – ThW Feb 02 '15 at 20:27
  • Hi, I have changed database connection encoding to utf8_general_ci, still ? marks :/ IDK why – Ammar Brohi Feb 03 '15 at 10:20
  • Are the strings correct if you open the table in your database ui (phpMyAdmin)? If you had the wrong encoding they might have been destroyed on save. This is difficult to debug without access to your system, but this answer might help you: http://stackoverflow.com/questions/279170/utf-8-all-the-way-through/279279#279279 – ThW Feb 03 '15 at 10:34
  • Thank you so much for helping, This XMLWriter helped me alot. This was backbone to solve the problem and I am done solving it. Thanks – Ammar Brohi Feb 03 '15 at 18:24