从数据库中检索阿拉伯字符显示问号

时间:2021-07-14 10:29:55

标签: php sql encoding utf-8 arabic-support

我正在使用 dr5hn 在此 GitHub countries.sql 中提供的 countries.sql 数据库。

我编辑了数据库,将名称列更改为 name_en 并在其后面添加了一个列,命名为 name_ar,用于名称的阿拉伯语翻译。这是编辑好的数据库。 countries.sql

我创建了一个名为 bb_data 的数据库,带有 utf8mb4_unicode_ci 排序规则。并导入编辑过的 countries.sql

enter image description here

这是配置文件

# ACTIVE TO REPORT ERRORS 
    error_reporting(E_ALL ^ E_NOTICE);
# SET PHP HEADER
    header('Content-Type: text/html; charset=utf8mb4');
# CONNECTION VARS
    $db_prefix              = ""; //name_[database_name]
    $db_server              = "localhost"; // leave it as it is, or contact your Techinical Support.
    $db_username            = "root"; // database_username
    $db_password            = "123"; //password of database_usersname
    $db_name                = "bb_data"; // database name

#SETUP
    // license
    $license                = "";
    // Charset
    $website_charset        = "utf8mb4"; 
    // language
    $default_language       = "ar";             // website default language

# CONNECTION 
    $mysqli = new mysqli($db_server,$db_prefix.$db_username,$db_password,$db_prefix.$db_name);
    $mysqli->set_charset("utf8mb4");

我正在从 courties.sql 中检索数据并将其打印到一个选定的 HTML 元素中。如下面的代码。

# PAGE PROCESS 
//Get All Countries and print out into select tags
$countries_option_list  = Null;
$name_column            = "name_" . $default_language;
$name_column            = $mysqli->real_escape_string($name_column);
$countries              = $mysqli->query("SELECT id,$name_column FROM countries") or die("countries print error");
while($row = $countries->fetch_assoc())
    $countries_option_list .= "<option value='".$row['id']."'>".utf8_encode($row[$name_column])."</option>";

HTML

这是在 HTML 页面的头部

<meta http-equiv='content-type' content='text/html; charset=<?=$website_charset?>' />

这是HTML元素代码

<select id="search_country" class="form-select mb-3" aria-label="example">
    <option value="" selected><?=lang("search_box_choose_country")?></option> // this will print Choose a country OR اختر الدولة based on the language of the client
    <?=$countries_option_list?> //PHP variables with option from last code will be printed here.
</select>

这是阿拉伯语的结果。

enter image description here

这是英文的结果。

enter image description here

我似乎找不到问题所在?

灵魂

在 PHP 代码中,我在测试解决方案时添加了 utf8_encode() 函数。我忘记删除它了。我做到了,问题解决了。

enter image description here

0 个答案:

没有答案
相关问题