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


Postgresql PHP invalid byte sequence for encoding UTF8

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

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

"Internal Server Error: SQLSTATE[22021]:
Character not in repertoire: 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'"

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

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

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

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

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

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

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

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

回答一个旧的帖子,但是,我刚刚有一个类似的情况,在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)";

希望对大家有所帮助

我将无法提交正确的unicode SQL-query (quercus for java从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));