<i id='IxpZe'><tr id='IxpZe'><dt id='IxpZe'><q id='IxpZe'><span id='IxpZe'><b id='IxpZe'><form id='IxpZe'><ins id='IxpZe'></ins><ul id='IxpZe'></ul><sub id='IxpZe'></sub></form><legend id='IxpZe'></legend><bdo id='IxpZe'><pre id='IxpZe'><center id='IxpZe'></center></pre></bdo></b><th id='IxpZe'></th></span></q></dt></tr></i><div id='IxpZe'><tfoot id='IxpZe'></tfoot><dl id='IxpZe'><fieldset id='IxpZe'></fieldset></dl></div>

<small id='IxpZe'></small><noframes id='IxpZe'>

  1. <tfoot id='IxpZe'></tfoot>
    <legend id='IxpZe'><style id='IxpZe'><dir id='IxpZe'><q id='IxpZe'></q></dir></style></legend>
    • <bdo id='IxpZe'></bdo><ul id='IxpZe'></ul>

    1. 如何让 MySQL 返回 UTF-8?

      时间:2023-10-03

      <small id='BFWlN'></small><noframes id='BFWlN'>

          <tbody id='BFWlN'></tbody>
      1. <i id='BFWlN'><tr id='BFWlN'><dt id='BFWlN'><q id='BFWlN'><span id='BFWlN'><b id='BFWlN'><form id='BFWlN'><ins id='BFWlN'></ins><ul id='BFWlN'></ul><sub id='BFWlN'></sub></form><legend id='BFWlN'></legend><bdo id='BFWlN'><pre id='BFWlN'><center id='BFWlN'></center></pre></bdo></b><th id='BFWlN'></th></span></q></dt></tr></i><div id='BFWlN'><tfoot id='BFWlN'></tfoot><dl id='BFWlN'><fieldset id='BFWlN'></fieldset></dl></div>
        • <bdo id='BFWlN'></bdo><ul id='BFWlN'></ul>

                <legend id='BFWlN'><style id='BFWlN'><dir id='BFWlN'><q id='BFWlN'></q></dir></style></legend>
              • <tfoot id='BFWlN'></tfoot>

                本文介绍了如何让 MySQL 返回 UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在使用 PHPUnit 来验证来自我的 PHP 代码的 XML 输出,但显然我在字符编码方面遇到了问题 MySQL 返回.这是我从 DOMDocument 得到的错误:

                I'm using PHPUnit to validate XML output from my PHP code, but apparently I have problems with the character encoding MySQL returns. Here is the error I get from DOMDocument:

                Input is not proper UTF-8, indicate encoding!
                Bytes: 0xE9 0x20 0x42 0x65
                

                我初始化了 DOMDocument 以使其使用正确的编码:

                I initialize the DOMDocument so it uses the correct encoding:

                $domDocument = new DOMDocument('1.0','UTF-8');
                

                当我使用 mb_detect_encoding 检查 saveXML() 的输出时,结果是 UTF-8.

                And when I check the output from saveXML() using mb_detect_encoding the result is UTF-8.

                我还检查了用于创建 XML 的所有调用,对遇到的所有 createCDATASection 参数使用 mb_detect_encoding,它们都是 UTF-8 或 ASCII(没有纯文本节点,所有内容都在 CDATA 块).

                I also checked all the calls used to create the XML, using mb_detect_encoding on all createCDATASection parameters encountered and they are all either UTF-8 or ASCII (there are no plain text nodes, everything is in CDATA blocks).

                我认为问题来自于使用é"字符(在 ISO 8859-1).将该字符添加到我的 XML 的行是:

                I think the issue comes from the use of an 'é' character (which is 0xE9 in ISO 8859-1). The line which adds that character to my XML is:

                $domDocument->createCDATASection($place->name);
                

                和 mb_detect_encoding($place->name) 给我 UTF-8.

                and mb_detect_encoding($place->name) gives me UTF-8.

                数据 ($place->name) 是从 MySQL 数据库中提取的.此数据库具有 UTF-8 字符集.

                The data ($place->name) is pulled from a MySQL database. This database has the UTF-8 charset.

                这是一些示例代码:

                $query = sprintf('SELECT name FROM place where id = 1');
                $result = mysql_query($query);
                $result = mysql_fetch_assoc($result);
                
                
                // -- Feeding UTF-8 data directly WORKS
                $domDocument = new DOMDocument('1.0','UTF-8');
                $rootNode = $domDocument->createElement('Response');
                $rootNode->appendChild($domDocument->createCDATASection('Café Belga'));
                $domDocument->appendChild($rootNode);
                
                $matcher = array('tag' => 'Response');
                self::assertTag($matcher, $domDocument->saveXML(), '', FALSE);
                
                // -- Feeding UTF-8 data from the resultset FAILS
                $domDocument = new DOMDocument('1.0','UTF-8');
                $rootNode = $domDocument->createElement('Response');
                $rootNode->appendChild($domDocument->createCDATASection($result['name']));
                $domDocument->appendChild($rootNode);
                
                $matcher = array('tag' => 'Response');
                self::assertTag($matcher, $domDocument->saveXML(), '', FALSE);
                

                在我的 PHPStorm 调试器中,从数据库中提取的字符串如下所示:

                In my PHPStorm debugger, the string fetched from the database looks like this:

                Café Belga

                所以我认为这是问题的根源.在 MySQLWorkbench 中,字符串是正确的:Café Belga.

                So I think that is the root of the problem. In MySQLWorkbench the string is correct: Café Belga.

                使用 utf8_encode($result['name']) 时,一切正常!

                When using utf8_encode($result['name']), however, everything works fine!

                在手表窗口中再检查一次:

                One more check in the watches window:

                mb_detect_encoding($result['name']) -> "UTF-8"

                mb_detect_encoding($result['name']) -> "UTF-8"

                mb_detect_encoding(utf8_encode($result['name'])) -> "UTF-8"

                mb_detect_encoding(utf8_encode($result['name'])) -> "UTF-8"

                顺便说一句,是否有任何网站可以让我简单地复制粘贴这些十六进制值并查看它们在不同字符集中应该是什么字符?

                On a side note, are there any sites where I can simply copy-paste those hex values and see what characters they are supposed to be in different character sets?

                推荐答案

                您必须将与数据库的连接定义为 UTF-8:

                You have to define the connection to your database as UTF-8:

                // Set up your connection
                $connection = mysql_connect('localhost', 'user', 'pw');
                mysql_select_db('yourdb', $connection);
                mysql_query("SET NAMES 'utf8'", $connection);
                
                // Now you get UTF-8 encoded stuff
                $query = sprintf('SELECT name FROM place where id = 1');
                $result = mysql_query($query, $connection);
                $result = mysql_fetch_assoc($result);
                

                这篇关于如何让 MySQL 返回 UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:fwrite() 和 UTF8 下一篇:在 PHP 5.3 和 Windows Vista 中使用日语文件名?

                相关文章

                最新文章

                <tfoot id='fBl5f'></tfoot>

                <small id='fBl5f'></small><noframes id='fBl5f'>

                  <legend id='fBl5f'><style id='fBl5f'><dir id='fBl5f'><q id='fBl5f'></q></dir></style></legend>
                1. <i id='fBl5f'><tr id='fBl5f'><dt id='fBl5f'><q id='fBl5f'><span id='fBl5f'><b id='fBl5f'><form id='fBl5f'><ins id='fBl5f'></ins><ul id='fBl5f'></ul><sub id='fBl5f'></sub></form><legend id='fBl5f'></legend><bdo id='fBl5f'><pre id='fBl5f'><center id='fBl5f'></center></pre></bdo></b><th id='fBl5f'></th></span></q></dt></tr></i><div id='fBl5f'><tfoot id='fBl5f'></tfoot><dl id='fBl5f'><fieldset id='fBl5f'></fieldset></dl></div>

                      <bdo id='fBl5f'></bdo><ul id='fBl5f'></ul>