$dbo = new PDO("mysql:host=localhost;dbname=database", "databaseuser",
"databasepassword", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$dbo -> exec("set character_set_client='utf8'");
$dbo -> exec("set character_set_results='utf8'");
$dbo -> exec("set collation_connection='utf8_general_ci'");
$prepSnapRetrieve = $dbo->prepare(
"SELECT * FROM znc_log WHERE `nick` LIKE :u AND `tstamp` BETWEEN :t1 AND :t2"
);
$prepSnapRetrieve->execute(array(':u' => str_replace("|osu","", $_GET['u']),
':t1' => $_GET['sns'], ':t2' => $_GET['sne']));
$snapshotListing = $prepSnapRetrieve->fetchAll();
echo("Snapshot of ".$_GET['u']. "'s chat on ".strftime("%e/%m/%g",$_GET['sne']));
echo("<br><br>");
foreach($snapshotListing as $chat) {
echo "<i>" . " " . "[" . strftime("%H:%M:%S", $chat['tstamp']) . "]" .
"</i> " . " ><u>" . $chat['channel'] . "</u>< " . "(<b>" .
htmlspecialchars($chat['nick'])."</b>) ".htmlspecialchars($chat['message']);
echo("<br>");
}
該片段的問題是代碼應該解析和已經存儲在輸出IRC數據UTF-8 - 但在呼應,任何Unicode內容簡單地顯示爲:PHP:從MySQL數據庫輸出UTF8使用PDO
??????
我已經回顧了幾個問題,幷包含了幾個聲稱的修復程序,但似乎沒有任何工作。另一個使用相同數據庫的腳本完美地迴應了UTF-8的內容,但它使用PHP的標準MySQL實現,而不是PDO。我究竟做錯了什麼?
您確定這不是客戶端問題嗎? Content-Type頭是否正確? – 2012-07-10 11:59:00
一般疑難解答指南:[在Web應用程序中處理Unicode前後](http://kunststube.net/frontback/) – deceze 2012-07-10 11:59:43
除了從kgb提示看看這裏http://stackoverflow.com/questions/1566602/IS-設置字符集-UTF8-必要的。可能是一個很好的開始......你的UTF8設置可能會在你的PDO/exec調用中得到重置 – 2012-07-10 12:13:37