使用PHP从数据库中获取波斯文本

时间:2017-12-31 11:14:53

标签: php database mysqli

我从数据库获取数据时遇到问题。波斯语单词显示为???????等问号。我怎么能避免这个?

这是我的代码:

header('Content-Type: text/html; charset=utf-8');
$conn = mysqli_connect("localhost", "somevalue", "somevalue", "somevalue");
$output = array();
$query  = "SELECT * FROM af_home ORDER BY home_id DESC LIMIT 50";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_array($result)) {
        $output[] = $row;
    }
    echo json_encode($output);
}

2 个答案:

答案 0 :(得分:1)

确保af_home列字符集是数据库中的UTF-8

答案 1 :(得分:0)

您可以转换数据库UTF-8:

ALTER DATABASE your_database CHARACTER SET utf8;
-- for each table:
ALTER TABLE some_table CONVERT TO CHARACTER SET utf8;

并确保该表为UTF8:

SET CHARACTER SET, SET NAMES, SET COLLATION_CONNECTION