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

标签 php postgresql pdo

我有一个简单的 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/

相关文章:

php - 如何调试 PHP 的内部 C 代码?

javascript - 文本中断javascript中的单引号

php - 如何通过 SIP 发送即时消息

sql - 在 Postgresql 中使用排名函数的第三高薪水

php - FuelPHP "invalid data source name"错误

php 如果某项等于 x、y 或 z

postgresql - 从 Postgres 字段中的任意多个值中选择

sql - 在表级和列级添加 PRIMARY KEY 约束有什么区别?

php - Uncaught Error : Call to a member function userDetails() on null

php - 在数据库中创建可用表的下拉列表