简体   繁体   English

如何使用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站点”添加到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>

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. 请注意,php提供了一个pg_set_client_encoding()来更改编码,但是,就像直接执行该操作的直接sql查询一样,它后端编码转换为请求的客户端编码,并且对插入没有帮助。 For that to work, the database/postreSQL must have the correct encoding set (see the first two references). 为此,数据库/ 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处理排序规则好得多,因此,如果您相距不远,并且需要多个排序规则,则切换为一个好主意)

In my opinion, PG stores the values correctly, since 32763 equals hex 7FFB equals 翻 ( wiki ) 我认为PG可以正确存储值,因为32763等于十六进制7FFB等于翻( wiki

Probably you have a problem displaying the data? 可能您在显示数据时遇到问题? Is there a separate Unicode-enabled datatype for string columns? 字符串列是否有单独的启用Unicode的数据类型? Did you check with pgAdmin what is the actual contents of your table? 您是否使用pgAdmin检查了表的实际内容?

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. 添加适当的<meta>标记或发送带有charset参数的Content-Type标头。


BTW: you've got SQL injection vulnerability in the code. 顺便说一句:您的代码中存在SQL注入漏洞。 Don't put request variables in queries. 不要在查询中放入请求变量。 Use prepared statements or at least always use pg_quote() . 使用准备好的语句,或者至少始终使用pg_quote()

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

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