无法使用 PHP 在 MYSQL 数据库中存储阿拉伯语 [英] Unable to Store Arabic in MYSQL database using PHP

查看:18
本文介绍了无法使用 PHP 在 MYSQL 数据库中存储阿拉伯语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几个小时我一直在努力寻找解决方案,但未能如愿.我已经在互联网和这个论坛上尝试了所有可能的解决方案,但似乎没有任何效果.问题来了.

我想存储用户将在 mysql 数据库中表单的 textarea 中键入或粘贴的阿拉伯语数据.该网站是阿拉伯语,表单仅包含此文本区域.

要点:

该网站正确显示静态阿拉伯语内容.整个网站都使用 utf8 字符集.所以这不是问题.

表 (home) 和内容字段 (content) 都设置为 utf8_unicode_ci 排序规则,所以这也不是问题.

Form 设置为 accept-charset='utf-8' 所以这也不是问题.

当我在表中使用 phpMyAdmin 插入阿拉伯文本时,我完全没有问题.

事实上,当在网站上检索表格中的阿拉伯文本(通过 phpMyAdmin 输入)时,它显示得非常完美.

但是一旦在文本区域中输入阿拉伯语数据并发送到数据库以使用插入查询数据进行存储,就会转换为 ????

我正在使用

mysql_query("SET NAMES 'utf8'");mysql_query('SET CHARACTER SET utf8');

连接后,这根本不是问题.正如我之前所说,数据被检索到了.我已经上传了两张图片.证明我的问题.

我真的被困在这里,正如你所看到的,我已经采取了所有可能的步骤来解决这个问题,但这种行为真的很奇怪.请帮忙.

我用来将数据插入数据库的代码是:

 $content=$_POST["content"];回声$内容;$sql="更新主页设置内容='$content'";$result=mysql_query($sql) 或 die(mysql_error());

你可以看到第二个 echo 语句.我添加它来调试问题.数据在这个回声中得到了完美的展示.我想问题出在查询中.

解决方案

看起来服务器端的所有代码都运行正常.当浏览器将表单提交回服务器然后数据错误或被错误解释时,错误似乎发生了.

1) 请您确认您安装了多字节扩展.如果不这样做,PHP 可能无法正确解释浏览器中的字符.http://php.net/manual/en/book.mbstring.php

2) 您能否仔细检查一下该页面是否被浏览器实际选择为 UTF8.浏览器可能不确定页面使用的字符集,但仍然正确显示阿拉伯字符,因为它正在猜测如何显示它们.所以首先.

在您的 HTML 中,添加此元标记:

还要在脚本顶部添加此 PHP 标头:

header("Content-Type: text/html;charset=UTF-8");

3) 如果这些都不起作用,请您在浏览器中查看实际提交的表单,使用 Chrome 中的开发人员工具或 Firefox 中的 Firebug 来准确查看提交的内容.

例如我刚刚做了一个测试页面,将一些阿拉伯文本提交到我服务器上的测试页面.在请求中,我可以看到表单被编码为

内容长度:61测试=%26%231607%3B%26%231594%3B%26%231594%3B%26%231601%3B&go=

将文本拆分为我能看到的单个字符

%26%231607%3B%26%231594%3B%26%231594%3B%26%231601%3B

即阿拉伯字符正在为我正确编码 - 也许你会看到不同的东西.

I have been trying to find a solution for past few hours but unable to do so. I have tried each and every possible solution on the Internet and this forum but nothing seems to be working. Here is the problem .

I want to store arabic data that user will type or paste in the textarea of form in mysql database. The website is arabic and form only contains this one textarea.

Important Points:

The website is displaying static arabic content correctly. utf8 charset is used throughout the website. So it is not a problem.

The table (home ) and the content field(content) are both set to utf8_unicode_ci collation so it is not a problem either.

Form is set to accept-charset='utf-8' so that's not a problem either.

When I insert arabic text using phpMyAdmin in the table I don't have any problem at all with it.

In fact when arabic text (entered through phpMyAdmin) in the table is retrieved on the website it shows perfectly.

But as soon as arabic data is entered in text area and sent to DB to be stored using insert query data transforms into ????

I am using

mysql_query("SET NAMES 'utf8'"); 
mysql_query('SET CHARACTER SET utf8');

After Connection so it is not a problem at all. As I said earlier the data is retrieved all right. I have uploaded the two images. Demonstrating my problem.

I am really stuck here and as you can see I have taken all the steps possible to solve this as well but this behavior is really weird. Please help.

The code I am using to insert the data into db is:

 $content=$_POST["content"];
echo $content;
$sql="update home set content='$content'";
$result=mysql_query($sql) or die(mysql_error());

You can see the second echo statement. I added it to debug the problem. Data is shown perfectly in this echo. The problem rises in the query I suppose.

解决方案

It looks like all the code on the server side is working properly. The error seems to be happening when the browser submits the form back the server and then either the data is wrong or is mis-interpreted.

1) Please can you confirm that you have the Multi-Byte extension installed. If you don't then PHP will probably be failing to interpret the characters from the browser correctly. http://php.net/manual/en/book.mbstring.php

2) Can you double-check that the page is actually picked up as UTF8 by the browser. It is possible that the browser isn't sure what character set the page is in, but is still displaying the Arabic characters correctly because it's guessing how to display them. So first.

In your HTML, add this meta tag:

Also add this PHP header at top of the script:

header("Content-Type: text/html;charset=UTF-8");

3) If those don't work please can you look at the actual form submission in the browser, either using the developer tools in Chrome or Firebug in Firefox to see exactly what is being submitted.

e.g. I just did a test page to submit some Arabic text to a test page on my server. In the request I can see the form is encoded as

Content-Length: 61
test=%26%231607%3B%26%231594%3B%26%231594%3B%26%231601%3B&go=

Splitting the text into individual characters I can see

%26
%231607
%3B
%26
%231594
%3B
%26
%231594
%3B%26
%231601
%3B

i.e. the Arabic characters are being encoded correctly for me - perhaps you would see something different.

这篇关于无法使用 PHP 在 MYSQL 数据库中存储阿拉伯语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆