問題描述
我正在嘗試在 mysql 數(shù)據(jù)庫中存儲一些阿拉伯語數(shù)據(jù)我已將 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 連接排序規(guī)則設置為:utf8_general_ci
MySQL connection collation is set to: utf8_general_ci
數(shù)據(jù)庫和表的排序規(guī)則設置為:utf8_general_ci
Database and table collations are set to: utf8_general_ci
此外,在我的 php 代碼中,我使用了 $mysqli->set_charset("utf8")
函數(shù)來確保字符集設置為 ut8 但實際上沒有任何工作,我正在發(fā)布我的數(shù)據(jù)使用 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 查詢存儲數(shù)據(jù)時,所有字符都存儲在錯誤的字符集編碼中
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
有什么建議嗎?
推薦答案
奇怪,它應該管用!
在成功連接到數(shù)據(jù)庫后嘗試添加這些行:
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">
這篇關于使用php將阿拉伯語文本存儲在mysql數(shù)據(jù)庫中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!