views:

586

answers:

3

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

"& #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>
+1  A: 

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

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

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

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).

(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)

Jonathan Fingland
which collation i need to be use ?
coderex
---- PostgreSQL database dump---- Started on 2009-06-27 17:22:32SET client_encoding = 'UTF8';SET standard_conforming_strings = off;SET check_function_bodies = false;SET client_min_messages = warning;SET escape_string_warning = off;SET search_path = public, pg_catalog;SET default_tablespace = '';SET default_with_oids = false;---- TOC entry 1466 (class 1259 OID 71803)-- Dependencies: 3-- Name: table1; Type: TABLE; Schema: public; Owner: prasanth; Tablespace: --CREATE TABLE table1 ( id integer NOT NULL, japan character(1000));
coderex
+1  A: 

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

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?

devio
+1  A: 

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).

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


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().

porneL
yes surely, this code is for just for understanding only.. as i am new in PGSql.So it bit difficult to understand.i know php mysql and my main plat form is the same.:)And i did the HTML endoding method also i just want to know is there any difficulty when am using the DB search function.
coderex