使用php将阿拉伯语文本存储在mysql数据库中

2021-12-25 00:00:00 utf-8 character-encoding php mysql mysqli

我正在尝试在 mysql 数据库中存储一些阿拉伯语数据我已将 html 文档字符集设置为 'utf8'

I am trying to store some Arabic data in a mysql database I have set the html document charset to be 'utf8'

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

MySQL 字符集设置为:UTF-8 Unicode (utf8)

MySQL charset is set to: UTF-8 Unicode (utf8)

MySQL 连接排序规则设置为:utf8_general_ci

MySQL connection collation is set to: utf8_general_ci

数据库和表的排序规则设置为:utf8_general_ci

Database and table collations are set to: utf8_general_ci

此外,在我的 php 代码中,我使用了 $mysqli->set_charset("utf8") 函数来确保字符集设置为 ut8 但实际上没有任何工作,我正在发布我的数据使用 html 形式的 php 脚本,其中 enctype 是:enctype="multipart/form-data" 因为在这种形式中我还上传了一个图像

In addition in my php code I used the $mysqli->set_charset("utf8") function to ensure that the charset is set to be ut8 but nothing is actually working , I am posting my data to the php script using html form where the enctype is : enctype="multipart/form-data" because in this form I also upload an image

奇怪的是,当我直接在 mysql 中编写查询时,阿拉伯字符存储正确没有问题,但是当我使用 php 查询存储数据时,所有字符都存储在错误的字符集编码中

The weird thing is that when I write my query directly in mysql the Arabic characters are stored properly with no problem , but when I use a php query to store the data all the characters are stored in a wrong charset encoding

有什么建议吗?

推荐答案

奇怪,它应该管用!

在成功连接到数据库后尝试添加这些行:

Try adding these lines after a successful connection to the database:

$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");


例如:

$mysqli = @new mysqli('DB_HOST', 'DB_USER', 'DB_PASS', 'DB_NAME');

if($mysqli->connect_errno) die('Connection Error!');
else{
    $mysqli->query("SET NAMES 'utf8'");
    $mysqli->query("SET CHARACTER SET utf8");
}


并在您的 php 页面的 head 部分包含这些 META 标签:


And include these META tags in the head section of your php page:

<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

相关文章