用于编码 UTF8 的 Postgresql PHP 无效字节序列

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

Postgresql PHP invalid byte sequence for encoding UTF8

phppostgresqlpdo

提问by Ahmad

I have a simple SQL syntax for inserting to table. I'm using Postgresql 8.4 and already set Database encoding to be UTF8, and POSIX for Collation and Character type.

我有一个简单的 SQL 语法来插入到表中。我正在使用 Postgresql 8.4 并且已经将数据库编码设置为 UTF8,并将 POSIX 设置为排序规则和字符类型。

The query is fine if i run it under pgadmin3, but bring error if i execute in PHP.

如果我在 pgadmin3 下运行它,查询是好的,但如果我在 PHP 中执行它会带来错误。

"Internal Server Error: SQLSTATE[22021]:
Character not in reperttheitroade: 7 ERROR: 
invalid byte sequence for encoding \"UTF8\": 0xd85b\nHINT:
This error can also happen if the byte sequence does not match the encoding expected by the server,
which is controlled by \"client_encoding\"

So i tried to set NAMES and client_encoding from PHP(PDO), but still have the same problem

所以我试图从 PHP(PDO) 设置 NAMES 和 client_encoding,但仍然有同样的问题

$instance->exec("SET client_encoding = 'UTF8';");
$instance->exec("SET NAMES 'UTF8';");

pg_set_client_encoding($link, "UNICODE");my be work if i'm using native postgresql driver pg_pconnect, but currently i'm using PDO as Driver.

pg_set_client_encoding($link, "UNICODE");如果我使用本机 postgresql 驱动程序pg_pconnect,我会工作,但目前我使用 PDO 作为驱动程序。

and i also already set mb_internal_encoding('UTF-8');

我也已经设置了 mb_internal_encoding('UTF-8');

Is there any other way to fix this issue ?

有没有其他方法可以解决这个问题?

This error only appear when i trying to insert non ascii word like arabic or japanese word

此错误仅在我尝试插入非 ascii 单词(如阿拉伯语或日语单词)时出现

回答by Dragan Menoski

Try to encode into utf-8 with utf8_encode().

尝试使用 utf8_encode() 编码为 utf-8。

$query = "INSERT INTO student (id, firstName, lastName, age) VALUES (1, 'myFisrtName', 'myLastName', 23)";

pg_exec($connection, utf8_encode($query ));

回答by Mani Dwivedi

Answering on an older post but, I just had a similar situation, during a CSV import, I noticed the error: invalid byte sequence for encoding "UTF 8": 0x95 in ....

I've fixed the error by just converting encoding from Windows-1252 to UTF-8 in PHP by using: mb_convert_encoding($fieldValue,'UTF-8','Windows-1252')

在较旧的帖子上回答但是,我刚刚遇到了类似的情况,在 CSV 导入期间,我注意到了错误: invalid byte sequence for encoding "UTF 8": 0x95 in ....

我通过使用以下命令将编码从 Windows-1252 转换为 PHP 中的 UTF-8 修复了错误: mb_convert_encoding($fieldValue,'UTF-8','Windows-1252')

    $query = "INSERT INTO student 
              (id, firstName, lastName, age) 
              VALUES 
              (1, '".mb_convert_encoding($firstName,'UTF-8','Windows-1252')."', 
                  '".mb_convert_encoding($lastName,'UTF-8','Windows-1252')."', 23)";

Hope this will help someone.

希望这会帮助某人。

回答by user3225497

I'am will be can't submit correct unicode SQL-query (quercusfor javavary bad work from unicode and all like "SET NAMES 'UTF8';" no working) , and I resolve this from Base64 convertation:

我将无法提交正确的 unicode SQL 查询(用于java 的quercus与 unicode 的工作不同,所有类似“SET NAMES 'UTF8';”都不起作用),我从Base64 转换中解决了这个问题:

$name_enc = base64_encode($name);       
$res = $db->prepare(
            'INSERT INTO "MyTable"("ID", "Name") VALUES
               (   nextval(\'gen_addresses\'), 
                   convert_from(decode(?, \'base64\'), \'UTF8\'));' 
     )->execute(array($name_enc));