2
我試圖做簡單的下列字符:PHP無法讀取UTF8
<?php
header("Content-Type: text/html;charset=UTF-8");
$con=mysqli_connect("localhost","dsdsds","test1234","dsdsds");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM discounts");
while($row = mysqli_fetch_array($result))
{
echo $row['name'] . " " . $row['rate'];
echo "<br>";
}
?>
當我這樣做,我的輸出顯示爲??????。在數據庫表中,它正確顯示爲阿拉伯語。我究竟做錯了什麼?另外我知道我應該添加代碼來將mysqli設置爲utf8,當我這樣做時,它會中斷腳本。
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
期待您的支持
這聽起來很愚蠢,但你檢查過你的文件是用UTF-8編碼嗎?你也可以嘗試在連接後立即運行這個查詢:'SET NAMES utf8;' – nstCactus
它打印什麼錯誤? –
是你的數據庫表utf-8編碼和數據看起來好嗎? – Aris