简体   繁体   English

Postgresql php和bytesequence问题

[英]Postgresql php and bytesequence issue

I have set the html meta tag to uft8 Also the form charset to utf8 To be uber sure i have run the php function utf8_encode() on the offending string 我已经将html meta标记设置为uft8,也将字符集设置为utf8。为了确保我已经在有问题的字符串上运行了php函数utf8_encode()

even after all this i am getting a postgres error when executing the query via the php function pg_query. 即使所有这些,通过php函数pg_query执行查询时,我也收到了postgres错误。

Yet when i copy and paste the query into a command line it executes fine. 但是,当我将查询复制并粘贴到命令行时,它可以正常执行。

The query (or the offending part at least): INSERT INTO locations (... alt_thorfare_name ... ) VALUES (... 'BÓTHAR GHLEANN/SHEÁIN' ...); 查询(或至少有问题的部分):INSERT INTO location(... alt_thorfare_name ...)VALUES(...'BÓTHARGHLEANN /SHEÁIN'...);

The error: Query failed: ERROR: invalid byte sequence for encoding UTF8: 0xd354 HINT: This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by client_encoding 错误:查询失败:错误:编码的字节序列无效UTF8:0xd354提示:如果字节序列与服务器期望的编码不匹配,该错误也可能发生,该编码由client_encoding控制

The problem being the fada (acute accent from the irish language) on the O and A. 问题是O和A上的fada(爱尔兰语的重音)。

I am totally stuck on what to do next to even attempt finding a solution. 我完全陷于下一步该怎么做甚至尝试寻找解决方案。 Any help appreciated 任何帮助表示赞赏

That error occurs because you're not actually passing UTF-8 data to pg_query . 发生该错误是因为您实际上没有将UTF-8数据传递给pg_query

0xd354 is not actually a valid UTF-8 sequence, it's an ISO-8859-1 sequence for "ÓT". 0xd354实际上不是有效的UTF-8序列,它是“ÓT”的ISO-8859-1序列。 See: 看到:

header("Content-type: text/plain");
$s = "ÓT";
$utf8 = mb_convert_encoding($s, "UTF-8", "HTML-ENTITIES");
$u = unpack("H*", $utf8);
echo "UTF-8 byte sequence: " . reset($u),"\n";
$iso8859 = mb_convert_encoding($s, "ISO-8859-1", "HTML-ENTITIES");
$u = unpack("H*", $iso8859);
echo "ISO-8859-1 byte sequence: " . reset($u),"\n";

gives

UTF-8 byte sequence: c39354
ISO-8859-1 byte sequence: d354

Do this: 做这个:

  • Don't use utf8_encode() unless maybe you get ASCII data from somewhere (external plain text file, for example). 除非您可能从某个地方(例如,外部纯文本文件)获取ASCII数据,否则不要使用utf8_encode() )。
  • Encode your PHP files as UTF-8 without BOM . 将您的PHP文件编码为不带BOM的UTF-8

To be uber sure i have run the php function utf8_encode() on the offending string 为了确保我已经在有问题的字符串上运行了php函数utf8_encode()

This is where things go wrong, you don't have to encode utf8 to utf8, it's already utf8. 这是出问题的地方,您不必将utf8编码为utf8,它已经是utf8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM