使用 PHP 从 MySQL 数据库中获取特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15892610/
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 10:08:29  来源:igfitidea点击:

Getting special characters out of a MySQL database with PHP

phpmysqlutf-8character-encoding

提问by Jason Wood

I have a table that includes special characters such as ?.

我有一个包含特殊字符的表,例如 ?。

This character can be entered and viewed using phpMyAdmin and other software, but when I use a SELECT statement in PHP to output to a browser, I get the diamond with question mark in it.

这个字符可以用phpMyAdmin等软件输入和查看,但是当我在PHP中使用SELECT语句输出到浏览器时,我得到了带有问号的菱形。

The table type is MyISAM. The encoding is UTF-8 Unicode. The collation is utf8_unicode_ci.

表类型是 MyISAM。编码为 UTF-8 Unicode。排序规则是 utf8_unicode_ci。

The first line of the html head is

html头的第一行是

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

I tried using the htmlentities() function on the string before outputting it. No luck.

在输出之前,我尝试在字符串上使用 htmlentities() 函数。没运气。

I also tried adding this to php before any output (no difference):

我还尝试在任何输出之前将其添加到 php(没有区别):

header('Content-type: text/html; charset=utf-8');

Lastly I tried adding this right below the initial mysql connection (this resulted in additional odd characters being displayed):

最后,我尝试在初始 mysql 连接的正下方添加它(这会导致显示额外的奇数字符):

$db_charset = mysql_set_charset('utf8',$db);

What have I missed?

我错过了什么?

采纳答案by Tian Yong

Below code works for me.

下面的代码对我有用。

$sql = "SELECT * FROM chartest";
mysql_set_charset("UTF8");
$rs = mysql_query($sql);
header('Content-type: text/html; charset=utf-8');
while ($row = mysql_fetch_array($rs)) {
    echo $row['name'];
}

回答by Justin Warkentin

There are a couple things that might help. First, even though you're setting the charset to UTF-8 in the header, that might not be enough. I've seen the browser ignore that before. Try forcing it by adding this in the head of your html:

有几件事可能会有所帮助。首先,即使您在标头中将字符集设置为 UTF-8,这也可能不够。我之前看到浏览器忽略了这一点。尝试通过在你的 html 的头部添加这个来强制它:

<meta charset='utf-8'>

Next, as mentioned here, try doing this:

接下来,正如这里提到的,尝试这样做:

mysql_query ("set character_set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");

EDIT

编辑

So I've just done some reading up an playing around a bit. First let me tell you, despite what I mentioned in the comments, utf8_encode()and utf8_decode()will not help you here. It helps to actually understand UTF-8 encoding. I found the Wikipedia page on UTF-8very helpful. Assuming the value you are getting back from the database is in fact already UTF-8 encoded and you simply dump it out right after getting it then it should be fine.

所以我刚刚做了一些阅读和播放。首先让我告诉你,尽管我在评论中提到的,utf8_encode()utf8_decode()不会帮助你在这里。它有助于真正理解 UTF-8 编码。我发现UTF-8上的维基百科页面非常有帮助。假设您从数据库中获取的值实际上已经是 UTF-8 编码的,并且您只需在获取后立即将其转储出来,那么它应该没问题。

If you are doing anything with the database result (manipulating the string in any way especially) and you don't use the unicode aware functions from the PHP mbstringlibrary then it will probably mess it up since the standard PHP string functions are not unicode aware.

如果您正在对数据库结果进行任何操作(尤其是以任何方式操作字符串)并且您不使用 PHP mbstring库中的 unicode 感知函数,那么它可能会搞砸,因为标准的 PHP 字符串函数不是 unicode 感知的.

Once you understand how UTF-8 encoding works you can do something cool like this:

一旦你了解了 UTF-8 编码的工作原理,你就可以做一些很酷的事情:

$test = "?";
for($i = 0; $i < strlen($test); $i++) { 
    echo sprintf("%b ", ord($test[$i]));
}

Which dumps out something like this:

哪个转储出这样的东西:

11100010 10000100 10100010

That's a properly encoded UTF-8 '?' character. If you don't have a character like that in your data retrieved from the database then something is messed up.

这是一个正确编码的 UTF-8 '?' 特点。如果从数据库中检索的数据中没有这样的字符,那么事情就搞砸了。

To check, try searching for a special character that you know is in the result using mb_strpos():

要进行检查,请尝试使用以下命令搜索您知道在结果中的特殊字符mb_strpos()

var_dump(mb_strpos($db_result, '?'));

If that returns anything other than falsethen the data from the database is fine, otherwise we can at least establish that it's a problem between PHP and the database.

如果返回的结果不是false数据库中的数据就没有问题,否则我们至少可以确定这是 PHP 和数据库之间的问题。

回答by Saeed

you need to execute the following query first.

您需要先执行以下查询。

mysql_query("SET NAMES utf8");