I start my PHP file with the following code segment:
<?php
session_start();
header( 'Content-Type: text/html; charset: utf8' );
$dbh = new PDO( "mysql:dbname=stats;host=localhost", '****', '******', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC) );
?>
Later in the code, I'm tabulating data from MySQL table as follows:
<tr class='<?php echo $className[($i % $max)]; ?>'>
<td><?php echo $i++; ?></td>
<td onclick='popData("<?php echo htmlspecialchars($mod['Nick']); ?>");'><?php echo $mod['Nick']; ?></td>
<td><?php echo $mod['Count']; ?></td>
<td style='padding-left: 15px;'><?php echo $theDate; ?></td>
</tr>
This method outputs the correct result for all values except when the nick has special characters like the trademark symbol in it. A sample output can be shown as:
<tr class='class1'>
<td>7</td>
<td onclick='popData("hjpotter92");'>hjpotter92</td>
<td>2343</td>
<td style='padding-left:15px;'>11<sup>th</sup> April, 2011</td>
</tr>
<tr class='class1'>
<td>19</td>
<td onclick='popData("");'>hjpotter92®</td>
<td>1057</td>
<td style='padding-left:15px;'>28<sup>th</sup> November, 2011</td>
</tr>
<tr class='class1'>
<td>22</td>
<td onclick='popData("");'>hjpotter92™</td>
<td>942</td>
<td style='padding-left:15px;'>9<sup>th</sup> February, 2008</td>
</tr>
As you notice, the parameter for popData function is empty for the cases where nick has a special character suffix. If I add charset=utf8 to the connection DSN (MySQL version 5.5.28), the output is something like:
<tr class='class1'>
<td>19</td>
<td onclick='popData("hjpotter92®");'>hjpotter92®</td>
<td>1057</td>
<td style='padding-left:15px;'>28<sup>th</sup> November, 2011</td>
</tr>
Why am I observing the weird behaviour? This is happening only on this one page (I've created a completely new file and still getting the empty block inside the call to popData or as the output of htmlspecialchars()).
PS: I'm unsure whether it is a PDO/MySQL related trouble or the function htmlspecialchars is having some issues. No error/warning is logged in apache/php error logs.
EDIT
My table collation is utf8_general_ci