Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql PHP invalid byte sequence for encoding UTF8

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

like image 398
Ahmad Avatar asked Mar 06 '13 16:03

Ahmad


1 Answers

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 ));
like image 121
Dragan Menoski Avatar answered Sep 18 '22 14:09

Dragan Menoski