Oracle oci_connect AL32UTF8 php utf8 weird characters

2.6k views Asked by At

I've been scratching my head for a while now, and I'm hoping that someone can push me in the right direction.

I have an Oracle DB that contains account information like Name, Surname, etc., and if I run the following (pseudo-)code:

<?php

$db = "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myHost)(PORT=myPort)))(CONNECT_DATA=(SERVICE_NAME=myServiceName)))"; 

oci_connect("user","pass",$db,'AL32UTF8');

$query="

SELECT

//tried this
lastname AS LAST_NAME

//tried this:
(convert(lastname,'AL32UTF8')) AS LAST_NAME

FROM
    SomeDb
WHERE
    SomeId = 1234 
";

$stid = oci_parse($c1, $query);
oci_execute($stid);

$row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS);

echo $row['LAST_NAME']; //returns �¼

echo utf8_decode($row['LAST_NAME']); // returns ür

echo utf8_encode($row['LAST_NAME']); // returns ���¼

it returns ü characters back as: Ã�¼, while ë characters are being displayed correctly.

I've checked the NLS_CHARACTERSET of the Oracle server which is set to: AL32UTF8.

I also checked the Apache charset config:

curl -A "Mozilla/4.0" http://localhost -I

HTTP/1.1 200 OK
Date: Tue, 05 Sep 2017 13:08:01 GMT
Server: Apache/2.4.10 (Debian) 
Content-Type: text/html; charset=UTF-8

I tried to set internal coding:

mb_internal_encoding("UTF-8"); // this seemed to be default UTF-8.

I tried several setenv values, but it seems nothing really works. What could be the problem? Am i overlooking something?

Additional info:

PHP -v  
PHP 5.6.30-0+deb8u1

OCI8
OCI8 Version 2.0.8
1

There are 1 answers

0
Shaysus On BEST ANSWER

In addition; I tried the following piece of code to find the correct encoding which gave me always UTF-8. If i moved the UTF-8 further back in the array it always gave me ISO-8859-1. So it turned out useless.

$encodings= array( 'UTF-8', 'ASCII', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', 
        'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', 'ISO-8859-10', 
        'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16', 
        'Windows-1251', 'Windows-1252', 'Windows-1254');

echo mb_detect_encoding($string, $encodings, true);

Further research didn't come up with a cause or solution, so i decided to have the source characters in the Oracle database adjusted to the correct character.

I know it's not really the solution i wanted but since the amount of characters that were showing this weird translation were very low that was the quickest solution.

Thanks to all that have been thinking along with me.

Regards,

Frans