php-oci8 encoding issue

Ramazan picture Ramazan · Nov 2, 2012 · Viewed 11.4k times · Source

Similar questions have been asked, but the regular solutions do not work for me. Probably I am missing something. I am about to loose my mind :(((

As you can understand it from the title, I have an oracle database with non-ascii content. I want to query/display it with php using oci8.

First of all, the database is in ISO8859P9, i.e. the query

select parameter, value from v$nls_parameters where parameter in ('NLS_CHARACTERSET', 'NLS_LANGUAGE', 'NLS_TERRITORY')

returns

NLS_LANGUAGE        AMERICAN
NLS_TERRITORY       AMERICA
NLS_CHARACTERSER    WE8ISO8859P9

So I have set up NLS_LANG variable accordingly, i.e. getenv("NLS_LANG") returns "AMERICAN_AMERICA.WE8ISO8859P9".

I inserted AddDefaultCharset ISO-8859-9 to /etc/apache2/conf.d/charset file, and I always included <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-9"> in head section of html content of php files. So when I check via firebug, I see that the "Content-Type" header for a response is "text/html; charset=ISO-8859-9".

Lastly, I have a line as default_charset = "ISO-8859-9" in /etc/php5/apache2/php.ini file, and in the core section of the output phpinfo(), I see that default_charset is ISO-8859-9.

After all these stuff, when I retrieve a string with a character İ in it and echo it, it is shown as '?'. That is the problem!!! When I query select dump(nameofthatstring, 16)..., I see that its byte value is DD, i.e. correct according to 8859-9.

I am not sure whether it is relevant but, I will give a final detail: When I enter a character ı in a form and submit it via ajax in GET, it is converted to %FD, i.e. the 8859-9 corresponding value, no problem.

When I urldecode it and echo it, it is again ok, I see ı. But when I have a query with a string containing ı in its where clause (after receiving the ajax request), it returns empty set.

However, if I select the rows using CHR(253), the rows are selected correctly, but printing these rows again results in '?' characters.

I am really stuck. Any help will be highly appreciated. Thanks in advance.

EDIT:

$database = "(DESCRIPTION= (ADDRESS= (PROTOCOL=TCP)(HOST=XXXX)(PORT=1521)) (CONNECT_DATA= (SERVER=dedicated)(SERVICE_NAME=sname)))";

First, I have tried this:

oci_pconnect($username, $password, $database, "AMERICAN_AMERICA.WE8ISO8859P9");

But it did not solve, so I set NLS_LANG variable and changed to the following:

oci_pconnect($username, $password, $database);

Nothing changed...

Answer

Nelson picture Nelson · Nov 2, 2012

Try connecting like this:

putenv("NLS_LANG=AMERICAN_AMERICA.WE8ISO8859P9");
$db = "(DESCRIPTION=
     (ADDRESS_LIST=
       (ADDRESS=(PROTOCOL=TCP)
         (HOST=$dbhost)(PORT=$dbport)
       )
     )
       (CONNECT_DATA=(SID=$dbname))
 )";
 $conn = OCILogon($dbuser, $dbpasswd, $db);

Set the variables $db* according to your configuration.