我有一个简单的 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\"
所以我尝试从 PHP(PDO) 设置 NAMES 和 client_encoding,但仍然有同样的问题
$instance->exec("SET client_encoding = 'UTF8';");
$instance->exec("SET NAMES 'UTF8';");
pg_set_client_encoding($link, "UNICODE");
如果我使用 native postgresql 驱动程序 pg_pconnect
,我会工作,但目前我使用 PDO 作为驱动程序。
我也已经设置了 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 ));
关于php - 用于编码 UTF8 的 Postgresql PHP 无效字节序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15252243/