如何使用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 联系我们: