php/mysql中阿拉伯语出现“????” html中的问号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13439324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:26:33  来源:igfitidea点击:

Arabic language in php/mysql appears "????" question marks in html

phpmysqlencodearabichtml-encode

提问by CairoCoder

Possible Duplicate:
Save Data in Arabic in MySQL database

可能的重复:
在 MySQL 数据库中以阿拉伯语保存数据

I have a problem with retrieving Arabic data from MYSQL database using PHP, it appears as question marks "????" in HTML:

我在使用 PHP 从 MYSQL 数据库中检索阿拉伯语数据时遇到问题,它显示为问号“????” 在 HTML 中:

  1. I have a database with "utf8_general_ci" as collation.
  2. The database contains some data in Arabic Language.
  3. The HTML encoding is "UTF-8".
  4. When I tried to retrieve the data in HTML, it appears as "?????".
  1. 我有一个以“utf8_general_ci”作为排序规则的数据库。
  2. 该数据库包含一些阿拉伯语数据。
  3. HTML 编码为“UTF-8”。
  4. 当我尝试检索 HTML 中的数据时,它显示为“?????”。

Please Help !!!

请帮忙 !!!

回答by A1Gard

you must set charset in first connect with mysql by this query:

您必须通过以下查询在第一次与 mysql 连接时设置字符集:

SET CHARACTER SET utf8

for example in mysqli functions

例如在 mysqli 函数中

$MySQL_Handle = mysqli_connect(HOSTNAME,DATABASE_USERNAME,DATABASE_PASSWORD,DATABASE_NAME) 
or die ( mysqli_error($MySQL_Handle) ); 

$sSQL= 'SET CHARACTER SET utf8'; 

mysqli_query($MySQL_Handle,$sSQL) 
or die ('Can\'t charset in DataBase'); 

and PDO sample :

和 PDO 示例:

$dbh = new PDO('mysql:host=localhost;dbname=' . $DB_NAME, $DB_USER,
$DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
$dbh->exec("SET CHARACTER SET UTF8");

this action need before insert and before select.

此操作需要在插入之前和选择之前。