12

I have a simple SQL syntax for inserting to table. I'm using Postgresql 8.4 and already set Database encoding to be UTF8, and POSIX for Collation and Character type.

The query is fine if i run it under pgadmin3, but bring error if i execute in PHP.

"Internal Server Error: SQLSTATE[22021]:
Character not in repertoire: 7 ERROR: 
invalid byte sequence for encoding \"UTF8\": 0xd85b\nHINT:
This error can also happen if the byte sequence does not match the encoding expected by the server,
which is controlled by \"client_encoding\"

So i tried to set NAMES and client_encoding from PHP(PDO), but still have the same problem

$instance->exec("SET client_encoding = 'UTF8';");
$instance->exec("SET NAMES 'UTF8';");

pg_set_client_encoding($link, "UNICODE"); my be work if i'm using native postgresql driver pg_pconnect, but currently i'm using PDO as Driver.

and i also already set mb_internal_encoding('UTF-8');

Is there any other way to fix this issue ?

This error only appear when i trying to insert non ascii word like arabic or japanese word

3
  • You can set all of your link/database encoding to use utf-8 but if the original string is not utf-8 encoded it will still cause an error. Commented Mar 6, 2013 at 16:13
  • Can you please post the result of SHOW client_encoding; from pgAdmin ? Commented Mar 6, 2013 at 20:34
  • 1
    Please show a single-character input and the corresponding byte sequence from the error report (like 0xd85b). You should also tell us what your operating system default text encoding is; if you don't know, run the locale command if you're on Linux/unix. Without knowing the original encoding and original text it's hard to say much. Commented Mar 6, 2013 at 23:41

3 Answers 3

11

Try to encode into utf-8 with utf8_encode().

$query = "INSERT INTO student (id, firstName, lastName, age) VALUES (1, 'myFisrtName', 'myLastName', 23)";

pg_exec($connection, utf8_encode($query ));
1
  • 4
    Encoding the whole query seems strange. (Also, your SQL example could be shorter and more correctly spelled :) Commented Feb 26, 2014 at 0:50
2

Answering on an older post but, I just had a similar situation, during a CSV import, I noticed the error: invalid byte sequence for encoding "UTF 8": 0x95 in ....

I've fixed the error by just converting encoding from Windows-1252 to UTF-8 in PHP by using: mb_convert_encoding($fieldValue,'UTF-8','Windows-1252')

    $query = "INSERT INTO student 
              (id, firstName, lastName, age) 
              VALUES 
              (1, '".mb_convert_encoding($firstName,'UTF-8','Windows-1252')."', 
                  '".mb_convert_encoding($lastName,'UTF-8','Windows-1252')."', 23)";

Hope this will help someone.

0
-1

I'am will be can't submit correct unicode SQL-query (quercus for java vary bad work from unicode and all like "SET NAMES 'UTF8';" no working) , and I resolve this from Base64 convertation:

$name_enc = base64_encode($name);       
$res = $db->prepare(
            'INSERT INTO "MyTable"("ID", "Name") VALUES
               (   nextval(\'gen_addresses\'), 
                   convert_from(decode(?, \'base64\'), \'UTF8\'));' 
     )->execute(array($name_enc));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.