如何使用PHP将日文文本插入Postgres表?

[英]How to insert Japanese text into a Postgres table using PHP?


i am tring to add "翻訳するテキストやWebページ " into a PostgreSQL table, but its shown like this:

我想把“翻译るすすWeb Web Web Web Web Web”“添加到PostgreSQL表中,但它显示如下:

"& #32763;""& #35379;"す& #12427;テ& #12461;& #12473;& #12488;& #12420;Web& #12506;& #12540;& #12472;

How can I insert that in proper format?

如何以正确的格式插入?

<?php
$db = pg_connect("host=localhost port=5432 dbname=lang user=password=") or die(":(");
pg_set_client_encoding($db , "UTF-8");
#pg_exec($db,"SET NAMES 'UTF-8'");
#pg_exec($db,"SET CLIENT_ENCODING TO 'UTF-8'");
//$lan=iconv("UTF-8",'ISO-8859-1//TRANSLIT',$_REQUEST['lan']);
$lan=$_REQUEST['lan'];
echo $lan;
if(array_key_exists('sub',$_REQUEST))
{
$sql="INSERT INTO table1 (japan) VALUES('{$lan}')";
pg_query($sql) or die("errot");
}
?>

<html>
<body>
  <form action="" method="">
    <input type="text" name="lan" />
    <input type="submit" name="sub" />
  </form>
</body>
</html>

3 个解决方案

#1


what you have will work as long as table1 has the right collation

只要table1具有正确的排序规则,您所拥有的将会起作用

see http://www.postgresql.org/docs/8.1/static/sql-createdatabase.html for setting the encoding (database-wide)

请参阅http://www.postgresql.org/docs/8.1/static/sql-createdatabase.html以设置编码(数据库范围)

see http://www.postgresql.org/docs/8.1/static/multibyte.html for the character support available and how to use them

请参阅http://www.postgresql.org/docs/8.1/static/multibyte.html以获取可用的角色支持以及如何使用它们

edit
note that php provides a pg_set_client_encoding() to change the encoding, however, like the direct sql query that does the same, it converts from the backend encoding to the requested client encoding and doesn't help with inserts. For that to work, the database/postreSQL must have the correct encoding set (see the first two references).

编辑注意php提供了一个pg_set_client_encoding()来改变编码,但是,就像直接sql查询一样,它会从后端编码转换为请求的客户端编码,并且对插入没有帮助。为此,数据库/ postreSQL必须具有正确的编码集(请参阅前两个引用)。

(note: mysql handles collations much better so if you aren't too far along and you require multiple collations then it may be a good idea to switch)

(注意:mysql处理校对的情况要好得多,所以如果你不太远,你需要多个校对,那么切换可能是个好主意)

#2


In my opinion, PG stores the values correctly, since 32763 equals hex 7FFB equals 翻 (wiki)

在我看来,PG正确存储值,因为32763等于十六进制7FFB等于翻(维基)

Probably you have a problem displaying the data? Is there a separate Unicode-enabled datatype for string columns? Did you check with pgAdmin what is the actual contents of your table?

显示数据可能有问题吗?字符串列是否有单独的启用Unicode的数据类型?你用pgAdmin检查了你桌子的实际内容吗?

#3


It seem that the issue is not related to the database at all.

似乎问题根本与数据库无关。

Simply your HTML lacks encoding declaration (in practice there's no reliable default encoding for HTML and you will get garbage).

只是你的HTML缺少编码声明(实际上,没有可靠的HTML默认编码,你会得到垃圾)。

Add appropriate <meta> tag or send Content-Type header with charset parameter.

添加适当的 标记或使用charset参数发送Content-Type标头。


BTW: you've got SQL injection vulnerability in the code. Don't put request variables in queries. Use prepared statements or at least always use pg_quote().

顺便说一句:你的代码中有SQL注入漏洞。不要将请求变量放在查询中。使用预准备语句或至少始终使用pg_quote()。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/06/27/3084a88851ee13856ab23a3a1b1f97a6.html



 
  © 2014-2022 ITdaan.com 联系我们: