phporacle-databaseoci8

php putenv() return inconsistent value


I have a PHP that connect to an Oracle database, and I need to set the NLS_LANG variable. In my PHP I set it with:

putenv("NLS_LANG=American_America.UTF8");
$oracle = $this->load->database('oracle', TRUE);

//do some query after this

But the value returned is inconsistent. One time it return

array(1) { [0]=> object(stdClass)#17 (1) { ["USERENV('LANG')"]=> string(2) "US" } } //this is correct

And a few refresh later it return

array(1) { [0]=> object(stdClass)#17 (1) { ["USERENV('LANG')"]=> string(2) "GB" } } //this is from my PC language setting

The query I use to check the LANG is :

SELECT userenv('LANG') FROM DUAL;

Is there any way to make it work without changing my PC language?

My configuration is :

Note : I also have tried using the $_ENV but the result also inconsistent.


Solution

  • The variable should be set before PHP starts. From the OCI8 documentation:

    Before starting the web server, OCI8 typically requires several Oracle environment variables (see below) to locate libraries, point to configuration files, and set some basic properties such as the character set used by Oracle libraries. The variables must be set before any PHP process starts.

    This is because setting them after the process starts results in behavior that only an expert can predict.

    To wind back, you probably want to use AL32UTF8 which is Oracle's name for UTF 8. Then, from the oci_connect() manual page, you can pass the character set as a parameter - and this is recommended:

    Passing this parameter can reduce the time taken to connect.

    Unless you need to override the country & territory components, you can probably let them default.

    If you do need to set NLS_LANG, do this in some Windows system environment setting so that when Apache starts, it has the value available.