Re: PHP + PostgreSQL: invalid byte sequence for encoding "UTF8"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 7/21/07, aldnin <aldnin@xxxxxxxx> wrote:> When I try to send this query (select 'lacarrière' as test;) to a UTF8 initialized pgsql-database (8.2.4) from PHP 5.2.3 I get this error:>> ERROR:  invalid byte sequence for encoding "UTF8": 0xe87265
Short answer: start using utf-8 for just everything, and your problemswill be gone.
Long explanation:This is usually the case when you get data from a form and put it inthe database, and the two aren't using the same encoding.I guess your pg connection is using unicode (so the db expects unicodeinput), and your html is set to something else. To fix this, you havetwo choices:
1-Run utf8_encode() on the input from your forms; or2-Set all your html pages to use utf-8 encoding.
IMHO, option 2 is the way to go. I've been using utf-8 for everythingfor quite some time, and has solved all my problems dealing withaccents, and so on.You will need:- All your HTML files encoded to utf-8 (quite easy with iconv, if youare using Linux);- Add a "Content-type: text/html; charset=utf-8" to all your pages.This is easily done using PHP's header() function in a file includedby all your scripts.
This way, the pages will be unicode, any data entered will be postedas unicode, and you will have no problems sending them to a databasethat uses unicode.Forget the <meta> tag that sets the encoding. It's only used in casethe server doesn't send a Content-type header, which isn't the casenormally. By default, I think at least apache sends the content-typeas iso8859-1.
-- Bruno Lustosa <bruno@xxxxxxxxxxx>ZCE - Zend Certified Engineer - PHP!http://www.lustosa.net/

[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux